Permalink
Browse files

the closure approach was silly. Simple array checking now.

  • Loading branch information...
1 parent 6e45489 commit 6586388a4c5735ec47871601712a811446807a48 borg committed Nov 3, 2012
@@ -47,39 +47,6 @@ DS.attr = function(type, options) {
options: options
};
- var isArray = (type.search(/\[\]/g) > -1);
-
- if (isArray) {
- var arrayProxy = Ember.Object.create({
- change: function() {
- var content = Ember.copy(this.get('content'));
- this.get('model')
- .setProperty(this.get('key'), content);
- }.observes('content.@each')
- });
-
- return Ember.computed(function(key, value) {
- var data;
-
- if (!arrayProxy.get('key')) {
- var me = this;
- arrayProxy.reopen({
- key: key,
- model: me,
- content: me.get('_data.attributes.%@'.fmt(key))
- });
- }
-
- if (arguments.length === 2) {
- // the key contains '[]', so it can't be 'id'
- arrayProxy.set('content', value);
- } else {
- value = arrayProxy.get('content');
- }
- return value;
- }).property('data').meta(meta);
- }
-
return Ember.computed(function(key, value) {
var data;
@@ -88,6 +55,13 @@ DS.attr = function(type, options) {
this.setProperty(key, value);
} else {
value = getAttr(this, options, key);
+ if (Ember.isArray(value)) {
+ if (!value.hasObserverFor('[]')) {
+ value.addObserver('[]', this, function() {
+ this.setProperty(key, Ember.copy(value));
+ });
+ }
+ }
}
return value;
@@ -18,7 +18,7 @@ module("DS.arrayAttr", {
array = [
{ id: '1', name: "Scumbag Dale", problems: ['booze'] },
{ id: '2', name: "Scumbag Katz", problems: ['skag'] },
- { id: '3', name: "Scumbag Bryn", problems: ['laydeez'] }
+ { id: '3', name: "Scumbag Bryn",}
];
store = DS.Store.create({
@@ -37,7 +37,7 @@ module("DS.arrayAttr", {
Person = DS.Model.extend({
name: DS.attr('string'),
- problems: DS.attr('string[]')
+ problems: DS.attr('string[]', {defaultValue: []})
});
store.loadMany(Person, [1,2,3], array);
@@ -59,4 +59,10 @@ test('the record should become dirty when array properties change', function() {
get(dale, 'problems').pushObject('cash flow');
equal(get(dale, 'problems.length'), 2, 'less money mo problems');
equal(get(dale, 'isDirty'), true, 'the model should be dirty now.');
+
+ var bryn = store.find(Person, 3);
+ equal(get(bryn, 'isDirty'), false, 'Bryn should still be clean');
+ get(bryn, 'problems').pushObject('laydeez');
+ equal(get(bryn, 'problems')[0], 'laydeez', 'Default array should be present');
+ ok(get(bryn, 'isDirty'), 'Pushing an object to the default should make it dirty');
});

0 comments on commit 6586388

Please sign in to comment.