Created
September 23, 2012 22:13
-
-
Save tdreyno/3773229 to your computer and use it in GitHub Desktop.
Ember.js View and Model pooling
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
/** | |
* A View Pool | |
*/ | |
FS.ViewPool = Em.ArrayController.extend({ | |
content: [], | |
viewClass: null, | |
createViewOnEmpty: true, | |
takeView: function(content) { | |
var view = this.popObject(); | |
if (!view) { | |
Ember.assert('pool must have available view to take if "createViewOnEmpty" is false', !this.get("createViewOnEmpty")); | |
var viewClass = this.get("viewClass"); | |
if ('string' === typeof viewClass) { | |
viewClass = Ember.getPath(viewClass); | |
} | |
view = viewClass.create(content); | |
} | |
return view; | |
}, | |
returnView: function(view) { | |
view.set("content", null); | |
this.pushObject(view); | |
} | |
}); | |
/** | |
* A Model Pool/Store | |
*/ | |
FS.ModelPool = Em.ArrayController.extend({ | |
content: [], | |
nextTakeIndex: 0, | |
_contentDidChange: function() { | |
this.set("nextTakeIndex", 0); | |
}.observes("length"), | |
nextModel: function() { | |
var idx = this.get("nextTakeIndex"); | |
var model = this.objectAt(idx); | |
var nextIdx = idx + 1; | |
if (nextIdx >= this.get("length")) { | |
nextIdx = 0; | |
} | |
this.set("nextTakeIndex", nextIdx); | |
console.log(model + " l:"+this.get("length")+" i:"+idx+" n:"+nextIdx); | |
return model; | |
}, | |
randomModel: function() { | |
var idx = Math.floor(Math.random() * this.get("length")); | |
var model = this.objectAt(idx); | |
return model; | |
} | |
}); | |
/** | |
* A Model Pool/Store | |
*/ | |
FS.TypedModelPool = Em.ArrayController.extend({ | |
content: [], | |
filteredPool: {}, | |
filterProperty: null, | |
filterPropertyValues: [], | |
_contentDidChange: function() { | |
this._rebuildFiltered(); | |
}.observes("@each.is2x"), | |
init: function() { | |
this._super(); | |
this._rebuildFiltered(); | |
}, | |
_rebuildFiltered: function() { | |
var filterProperty = this.get("filterProperty"); | |
var filterPropertyValues = []; | |
if (Em.none(filterProperty)) { return; } | |
var filteredPool = this.reduce(function(result, item, i) { | |
var propVal = item.getPath(filterProperty).toString(); | |
// added 'all' type so we can just pull from the entire pool for the main grid | |
if (typeof result['all'] === "undefined") { | |
result['all'] = FS.ModelPool.create({ content: [] }); | |
filterPropertyValues.push('all'); | |
} | |
if (typeof result[propVal] === "undefined") { | |
result[propVal] = FS.ModelPool.create({ content: [] }); | |
filterPropertyValues.push(propVal); | |
} | |
result[propVal].pushObject(item); | |
result['all'].pushObject(item); | |
return result; | |
}, {}); | |
if (!Em.none( filteredPool['all'] )) { | |
var randomized = filteredPool['all'].get("content").sort(function() { return 0.5 - Math.random() }) | |
filteredPool['all'].set("content", randomized); | |
} | |
this.setProperties({ | |
filterPropertyValues: filterPropertyValues.uniq(), | |
filteredPool: filteredPool | |
}); | |
}, | |
nextModelOfType: function(type) { | |
var filterPool = this.get("filteredPool"); | |
var filterProperty = this.get("filterProperty"); | |
if (Em.none(filterProperty)) { return; } | |
if (typeof filterPool[type] == "undefined") { return; } | |
return filterPool[type].nextModel(); | |
}, | |
nextModel: function() { | |
var filterPool = this.get("filteredPool"); | |
return filterPool['all'].nextModel(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment