Last active
December 18, 2015 14:28
-
-
Save dspangen/5797021 to your computer and use it in GitHub Desktop.
Mixins blogpost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DigitalAssetsCollection = Backbone.Collection.extend({ | |
model: Backbone.Model, | |
initialize: function(undefined, options) { | |
this.parent = options && options.parent; | |
if ( !this.parent ) { | |
throw "a parent product must be provided at initialization"; | |
} | |
_.bindAll(this, "url"); | |
}, | |
// Uses the path of the parent to generate the url for this digital | |
// assets collection | |
url: function() { | |
return _.result(this.parent, "url") + "/digital_assets"; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DigitalAssetsCollection = Backbone.Collection.extend({ | |
model: Backbone.Model, | |
mixins: [ NestedModelMixin ] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var NestedModelMixin = { | |
initialize: function(undefined, options) { | |
this.parent = options && options.parent; | |
if ( !this.parent || ! this.nestedPath ) { | |
throw "a parent model, corresponding to the parent of this Nested, " + | |
"must be provided at initialization, as well as the nestedPath"; | |
} | |
_.bindAll(this, "url"); | |
}, | |
// Path to query against is the root path of the parent model, plus the collection key, e.g. | |
// [/products/999] + [/digital_assets] would be /products/999/digital_assets; query parameters | |
// returned by `params` below would then be tacked on, if returned. | |
url: function() { | |
return _.result(this.parent, "url") + "/" + this.nestedPath; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment