hay / stapes

the Javascript MVC microframework that does just enough
http://hay.github.io/stapes
MIT License
443 stars 54 forks source link

Collections with observable properties #21

Closed yocontra closed 12 years ago

yocontra commented 12 years ago

Is it by design that push and set add data to the same place? coll.set('something', 'something') after doing coll.push causes all of the data to be mixed together which causes issues when trying to do data binding on collections. Is there a recommended way to do collections without having to reimplement a lot of the core stapes stuff?

hay commented 12 years ago

That seems weird. Could you make a testcase and attach it? Like to see what you're doing to see if it's a bug or a feature :)

yocontra commented 12 years ago
test("should not mix array and property values", function() {
    var module = Stapes.create();
    module.push([
       'value1',
       'value2',
       'value3'
    ]);

    module.set('stray', true);
    var values = [];
    module.each(function(value, key) {
        values.push(value);
    });
    deepEqual(values, ['value1', 'value2', 'value3'], "should only show array items");
});

This is less of a problem and more of a feature request for a way to keep pushed items and properties separated

hay commented 12 years ago

Yes, this is indeed by design. push() is just a shortcut for doing a set with a generated id. I would recommend you either

1) make a 'collection-only' Stapes module (and only use push).

2) If you don't want change events simply add an array as a property

var Module = Stapes.create().extend({
    "init" : function() {
        this.items = [];
    },

    "add" : function(name) {
        this.items.push(name);
    }
});

3) Use two seperate Stapes sub-objects in your model, one for collections, one for attributes

var Module = Stapes.create().extend({
    "collection" : Stapes.create(),
    "attributes" : Stapes.create()
});

Module.collection.push( "item" );
Module.attributes.set('name', 'Johnny');