Created
November 29, 2012 19:55
-
-
Save torifat/4171435 to your computer and use it in GitHub Desktop.
RivetsJS Adapter for BackboneJS with RequireJS
This file contains 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
define(['rivets', 'backbone'], function(rivets, Backbone){ | |
rivets.configure({ | |
adapter: { | |
subscribe: function(obj, keypath, callback) { | |
if (obj instanceof Backbone.Collection) { | |
obj.on('add remove reset', function () { | |
callback(obj[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[keypath]); | |
}); | |
} else { | |
obj.off('change:' + keypath, function (m, v) { | |
callback(v); | |
}); | |
} | |
}, | |
read: function(obj, keypath) { | |
if (obj instanceof Backbone.Collection) { | |
// keypath = null | |
return obj['models']; | |
} else { | |
return obj.get(keypath); | |
} | |
}, | |
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