Goal
To create an array of Model’s, managed by an ArrayController (ArrayProxy).
Requirements
Use ArrayController (ArrayProxy) abstraction to encapsulate the array of Model’s
Convert objects input to ArrayProxy automagically upon insertion into the ArrayProxy
Do not lazy convert at access time
Example Data Structures
App.AddressModel = Ember.Object.extend({
address_name: null,
address: null,
printme: function() {
console.log("Just making sure the array turned into an AddressModel");
},
});
App.addressArray = Ember.ArrayProxy.create({
transformFrom: function(item) {
},
transformTo: function(item) {
},
arrayContentWillChange: function(startIdx, removeAmt, addAmt) {
},
});
Failures by Trial
dynamic property
Someone in the IRC channel mentioned trying dynamic properties. This resulted in a, what appeared to be by logic and empirical evidence, a recursive result. No doubt from making content both the dynamically generated variable and the ‘trigger/exported’ variable.
arrayContentWillChange
Again, a seemingly recursive result. Upon receiving a arrayContentWillChange notification I generate an AddressModel from the given array index item(s). I then set the old indexed item to the created Model and an arrayContentWillChange event is triggered again, repeat … recurse.
transformFrom / transformTo
https://github.com/emberjs/ember.js/pull/554#issuecomment-5401112
tomdale mentions in the above post to try using transformFrom and transformTo to cast the incoming and/or outgoing data. These function don’t seem to exist [http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.8.js].
ArrayProxy model patch
https://github.com/emberjs/ember.js/pull/554
tomdale’s suggestion to the original solution/post to this problem seems to generalize better than the model implementation introduced by jedwood, however, in Backbone.js handles this problem by using the special model variable and I found it to work well.
The Question
How do I extend ArrayProxy to convert all incoming object, to be managed, into an AddressModel?
I took the approach mentioned by Tom Dale which overwrites the
replacemethod. It also registers an observer for thecontentproperty to assure that the content is a typed version of the array, see http://jsfiddle.net/pangratz666/XCLmE/:This ModelArrayProxy can then be used as a “normal”
ArrayProxy: