Skip to content

Instantly share code, notes, and snippets.

@far-blue
Created November 5, 2015 13:45
Show Gist options
  • Save far-blue/bd85dfa7349fd3665645 to your computer and use it in GitHub Desktop.
Save far-blue/bd85dfa7349fd3665645 to your computer and use it in GitHub Desktop.
var OrderItemSet = Backbone.Collection.extend({
model: function(data, options)
{
var isAccountLevel = _.has(data._embedded.product, 'isAccountLevel')
&& data._embedded.product.isAccountLevel;
var subType = data.subType || null;
var orderItemFunction = OrderItem.factory(data.orderItemType, subType, isAccountLevel);
return new orderItemFunction(data, _.clone(options));
},
initialize: function()
{
// Massive hack needed for backbone 1.1.2 because it has a bug that means
// when a function is supplied for model you can't use custom id attributes.
// This is fixed in 1.2.0 by using the modelId attribute (see below) but at
// the time of writing nestedtypes was not working correctly with backbone 1.2.0
this.model.prototype.idAttribute = 'uid';
this.on('refetch', function(){_.defer(function(item){item.fetch()}, this);}, this);
},
// this is the correct way to specify the model id attribute when the model
// attribute is a factory function rather than a model constructor. This
// should work from backbone 1.2.0. See the hack in initialize for backbone 1.1.2.
modelId: function (attrs) {
return 'uid';
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment