azproduction / rivets-backbone-adapter

Backbone.js adapter for Rivets.js data-bind with nested models and collections support
http://azproduction.github.io/rivets-backbone-adapter/example/index.html
MIT License
48 stars 15 forks source link

decision whether return collection or internal array of model should be a confiugration option #22

Open stalniy opened 8 years ago

stalniy commented 8 years ago

I know in order to iterate over backbone collection adapter returns internal array instead of collection. That really annoying for cases when you need collection, for example when you pass it as parameter to component (as attribute)

So, I suggest to move such decision to configuration option:

rivets.adapters[':'].returnCollection = true;

// and inside rivets-backbone.js
if (!rivets.adapters[':'].returnCollection && value instanceof Collection) {
  return value.models;
}

cc: @azproduction @der-On @generalov @StephanHoyer @Jmeyering @jeron-diovis

stalniy commented 8 years ago

Also I created an issue in rivets repository. Fix of that will allow to iterate over backbone collections (and not only) https://github.com/mikeric/rivets/issues/583

stalniy commented 8 years ago

Or you could add something like this:

let eachBinderRoutine = rivets.binders['each-*'].routine;
rivets.binders['each-*'].routine = function(el, collection) {
    if (collection.models) {
        collection = collection.models;
    }

    return eachBinderRoutine.call(this, el, collection);
};
sarim commented 8 years ago

The linked issue (mikeric/rivets#583) and linked issue there proposes some alternative ways to solve this problem. Still i don't really like those solutions :(

When adapter returns Collection.models, i loose access to attributes (builtin or custom) defined at my Collection. So I did this,

        // rivets cant iterate over Backbone.Collection -> return Array
        if (value instanceof Collection) {
            value.models.parent = value;
            return value.models;
        }

This way i can check for it. obj.parent instanceof Backbone.Collection And access attrs using obj.parent.attr.