Forked from wulftone/backbone-rivets.config.js
Last active
December 13, 2015 19:49
-
-
Save lightman76/4965856 to your computer and use it in GitHub Desktop.
New to backbone and rivets - but in my initial experiments I noticed problems around collections and models with collection attributes. Think this version of the adapter better handles those than the original. I did see where someone posted that beyond trivial examples one should bind to the collection directly versus handling it directly under …
This file contains hidden or 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
rivets.configure({ | |
adapter:{ | |
subscribe:function (obj, keypath, callback) { | |
if (obj instanceof Backbone.Collection) { | |
obj.on('add remove reset', function () {callback(obj);}); | |
} else if(obj.get(keypath) instanceof Backbone.Collection) { | |
obj.get(keypath).on('add remove reset', function (m,v) {callback(obj.get(keypath));}); | |
} else { | |
obj.on('change:' + keypath, function (m, v) {callback(v)}); | |
} | |
}, | |
unsubscribe:function (obj, keypath, callback) { | |
if (obj instanceof Backbone.Collection) { | |
obj.off('add remove reset', function () {callback(obj);}); | |
} else if(obj.get(keypath) instanceof Backbone.Collection) { | |
obj.off('add remove reset', function () {callback(obj[keypath]);}); | |
} else { | |
obj.off('change:' + keypath, function (m, v) {callback(v)}); | |
} | |
}, | |
read:function (obj, keypath) { | |
if (obj instanceof Backbone.Collection) { | |
//need to bind to events on the collection as well if we haven't | |
if (keypath == "") return obj.models; | |
return obj[keypath]; | |
} else { | |
var ret = obj.get(keypath); | |
if (ret instanceof Backbone.Collection) { | |
ret = ret.models; | |
} | |
return ret; | |
} | |
}, | |
publish:function (obj, keypath, value) { | |
if (obj instanceof Backbone.Collection) { | |
obj[keypath] = value; | |
} else { | |
obj.set(keypath, value); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment