Skip to content

Instantly share code, notes, and snippets.

@ascsystems
Created September 12, 2013 19:57
Show Gist options
  • Select an option

  • Save ascsystems/6542983 to your computer and use it in GitHub Desktop.

Select an option

Save ascsystems/6542983 to your computer and use it in GitHub Desktop.
line 156 - Application.Section is not a constructor.
Application.Collection = Backbone.Collection.extend(
{
constructor : function () {
this.request = new Application.Request();
Backbone.Collection.apply(this, arguments);
},
responseModel : Application.Response,
responseEvents : {},
requestRoot : '_',
checkDub : false,
checkDubAfterInit : true,
isDub : function (b) {
var isDub = false;
var dubFn = function (a,b) {
if (!(a.idAttribute) || !(a.has(a.idAttribute))) {
return false;
}
if (a.get(a.idAttribute) && b[a.idAttribute]) {
var checkId = (a.idAttribute == '_id') ? b[a.idAttribute].$id : b[a.idAttribute]
return _.isEqual(a.get(a.idAttribute),checkId);
}
return false;
};
$.each(this.models, function () {
isDub = dubFn(this, b);
return !isDub;
})
return isDub;
},
/**
* default from server is under items.
* sets the response data
*/
parse : function (data) {
this.getResponse().set(data)
var self = this;
if (this.checkDub) {
var items = $.map(data.items, function (newModel) {
if (self.isDub(newModel)) {
return null;
}
return newModel;
})
return items;
}
return data.items;
},
_fetching : {},
fetchOne : function (id, success) {
var result = this.get(id);
if (typeof success !== 'function') {
success = function () {};
}
if (typeof result !== 'undefined') {
success.apply(result);
return result;
}
var c = this;
if (typeof c._fetching[id] !== 'undefined') {
c._fetching[id].push(success);
return;
} else {
c._fetching[id] = [success];
}
var where = {};
where[this.model.prototype.idAttribute] = id;
var model = new this.model(where);
this.add(model);
model.fetch({
success: function () {
$.each(c._fetching[id], function () {
this.apply(model);
});
}
});
return model;
},
/**
* getter for the respoonse model. Useful for triggering events
* when you load new data from the server
*
*/
getResponse : function () {
if (!this.response) {
var self = this;
var r = this.responseModel.extend();
this.response = new r();
$.each(this.responseEvents, function (event, fn) {
self.response.on(event, fn, self);
});
}
return this.response;
},
sendRequest : function (options) {
options = options ? _.clone(options) : {};
var data = options.data || {};
if (this.requestRoot) {
var r = {};
r[this.requestRoot] = this.request.toJSON();
} else {
var r = this.request.toJSON();
}
options.data = _.extend(r, data);
return this.fetch(options)
},
hasNextPage : function () {
return this.request.get('page') < parseInt(this.getResponse().get('total'));
},
nextPage : function (options) {
if (!(this.hasNextPage())) {
return false;
}
var p = this.request.get('page');
this.request.set('page', p+1);
return this.sendRequest(options);
}
});
Application.Collection.extend = Backbone.Collection.extend;
/**
* model tab model. Interacts with Application.Section
* namespace to add custom views
*
*/
Application.Tab.Model = Application.Model.extend({
idAttribute : '_id',
initialize : function (options) {
this.constructor.__super__.initialize.apply(this, [options]);
//@todo this might be better in validation?
if (!(this.has('section'))) {
$.error("No section");
}
this.on('change', function (m, e) {
var keys = ['manufAbbr'];
var resource = '';
var go = false;
if (e.changes) {
$.each(e.changes, function (k, changed) {
if (changed && $.inArray(k, keys) >= 0) {
go = true;
return false;
}
});
}
if (false === go) {
return;
}
$.each(keys, function (i, k) {
if (resource !== '') resource += '-';
resource+= m.get(k);
});
m.attributes.resourceId = resource;
})
this.view = new Application.Section[this.get('section')][this.get('tab')](this);
}
});
/**
* Tab collection
*/
Application.Tab.Collection = Application.Collection.extend({
url : '/tabs',
model : Application.Tab.Model
});
/**
* section namespace used by external sections
*/
Application.Section = {
/**
* Base Section view. Has some useful helper functions
* that can be overwritten
*/
View : Backbone.View.extend({
initCount : 0,
inits : ['createTab'],
hasInitialized : false,
/**
* Pass in a tabmodel, this will initialize the view, addes the tab.
*/
initialize : function (TabModel, options) {
this.tabModel = TabModel;
var self = this;
this.tabModel.on('change:focus', function (m, v) {
if (v === 1) {
self.focus();
}
})
},
nextInit : function () {
this.initCount = this.initCount + 1;
var fn = this.inits[this.initCount - 1];
if (typeof fn == 'string') {
if (this[fn]) fn = this[fn];
}
if (typeof fn !== 'function') {
return;
}
fn.apply(this, arguments);
},
/**
* easy way to provide a child initalize function, do what you will...
*/
init : function (options) {
},
createTab : function () {
var self = this;
var section = this.tabModel.get('section');
var $tabs = $("#" + section)
.find(".sec-tabs").find('.tab-content:first');
var id = this.getTabId();
//.tabs("add", "#" +, this.getName(), this.tabModel.get('order') || 0);
this.$pane = $("<div>").attr({
id : id,
class: 'tab-pane ' + section + '-' + this.tabModel.get('resourceId'),
"data-slug" : this.tabModel.get('tab')
});
this.setElement(this.$pane);
$tabs.append(this.$pane);
this.$tab = $("<a>")
.attr({
href: '#' + id,
"data-toggle" : "tab",
"data-slug" : this.tabModel.get('tab')
})
.html(this.getName())
.appendTo($("<li>").appendTo($('#' + section).find(".sec-nav").find('.nav:first')))
//.appendTo($("<li>").appendTo($('#' + section).find(".sec-tabs").find('.nav-tabs:first')))
.click(function () {self.focus();});
/*
this.$tab = $(".ui-tabs-nav a[href=#" + this.getTabId() + "]").click(function () {
self.tabModel.set('focus', 1).save();
}).closest("li");
*/
this.nextInit();
if (this.tabModel.get('focus')) {
this.focus();
}
},
/**
* Getter for a name, this should be overwritten
*/
getName : function () {
return "Unkown Tab";
},
getTabId : function () {
var a = [this.tabModel.get('section'), this.tabModel.get('tab')]
if (this.tabModel.has('resourceId')) {
a.push(this.tabModel.get('resourceId'));
}
if (this.tabModel.has('_id')) {
a.push(this.tabModel.get('_id'));
}
return a.join('-');
},
/**
* Brings focus to this tab, agrates the tabModel's
* collection and unsets and tab's focus proberty.
*/
focus : function () {
var self = this;
if (!this.$pane) {
return;
}
if (!(this.hasInitialized)) {
this.hasInitialized = true;
this.init();
}
/*
$.scrollTo("#" + this.tabModel.get('section'), {
offset: -140
});
*/
this.$tab.tab('show');
var models = this.tabModel.collection.where({
section : self.tabModel.get('section'),
focus : 1
});
$.each(models, function () {
if (this.id != self.tabModel.id) {
this.set('focus', 0)
this.save();
}
});
this.tabModel.set('focus', 1);
this.tabModel.save();
},
filterManuf : function () {
var model = this.tabModel;
if (model.has('manufAbbr')) {
this.nextInit();
return false;
}
var self = this;
var filter = function (manuf, onFilter) {
if (typeof onFilter !== 'function') onFilter = function () {};
model.set('manufAbbr', manuf);
var check = model.collection.where({
section : model.get('section'),
tab : model.get('tab'),
manufAbbr : model.get('manufAbbr')
});
if (check.length > 0) {
for (var i in check) {
if (check[i].id !== model.id) {
model.destroy({success: function () {
var target = check[i];
target.set('focus', 1).save();
onFilter.apply(target.view, [false])
return false;
}})
return false;
}
}
}
model.save();
self.manuf = new Backbone.Model(session.manufs[model.get('manufAbbr')]);
onFilter.apply(self, [true]);
return true;
}
if (_.size(session.manufs) == 1) {
$.each(session.manufs, function (abbr) {
filter(abbr);
return false;
});
this.nextInit();
return;
}
var selected = function () {
var $$ = $(this).find('.btn-primary');
if ($$.length == 0) {
$(this).dialog('widget').find('.ui-dialog-title').effect('pulsate')
return false;
}
return $$.data('manuf');
}
var buttons = {};
if (this.manufPickerButtons) {
$.each(this.manufPickerButtons, function (label, fn) {
buttons[label] = function (e) {
fn.apply(this, [self, selected.apply(this), filter, e])
}
})
}
this.manufPicker =
$("<div>").addClass('manuf-picker').dialog({
modal : true,
title : 'Select a Manufacture',
width : $(window).width() - 100,
height : $(window).height() - 150,
self : this,
beforeClose : function (){
var manuf = selected.apply(this);
if (false === manuf) {
return false;
}
//return filter(manuf);
},
buttons : $.extend({}, {
Continue : function () {
var manuf = selected.apply(this);
if (false !== manuf) {
var success = filter(manuf);
if (success) self.nextInit();
$(this).dialog('close');
}
}
}, buttons)
});
$.each(session.manufs, function (abbr, manuf) {
$("<img>").attr({
src : 'http://www.k12foodservice.com/Images/Mfr_Logos/' + abbr + '.png',
width : 250
}).appendTo($("<span>").addClass('btn btn-large').attr({
title: manuf.Name,
'data-manuf' : abbr
}).appendTo(self.manufPicker).click(function () {
self.manufPicker.find('.btn-primary').removeClass('btn-primary');
$(this).addClass('btn-primary');
}).dblclick(function () {
self.manufPicker.dialog("close");
}));
})
}
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment