Created
September 5, 2014 02:59
-
-
Save HenrikJoreteg/e76d72b0559ae9b6061b to your computer and use it in GitHub Desktop.
Nested models in Ampersand.js
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
// credit for this goes to @latentflip | |
var State = require('ampersand-state'); | |
var Collection = require('ampersand-collection'); | |
var OptionList = Collection.extend({ | |
//Circular reference so we have to function wrap this | |
model: function (data, options) { | |
return new Option(data, options); | |
} | |
}); | |
var Option = State.extend({ | |
initialize: function () { | |
this.hasChildren = !!this.options.length; | |
}, | |
props: { | |
key: 'string', | |
title: 'string', | |
description: 'string', | |
isRoot: ['boolean', true, false], | |
hasChildren: 'boolean', | |
popular: ['boolean', true, false] | |
}, | |
collections: { | |
options: OptionList | |
}, | |
findByProperty: function (property, value) { | |
var result = []; | |
var self = this; | |
function findOptions(model) { | |
if (model[property] === value) { | |
result.push(model); | |
} | |
model.options.each(findOptions); | |
} | |
findOptions(this); | |
return result; | |
} | |
}); | |
OptionList.model = Option; | |
module.exports = Option; | |
module.exports.OptionList = OptionList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment