Skip to content

Instantly share code, notes, and snippets.

@lightman76
Forked from wulftone/backbone-rivets.config.js
Last active December 13, 2015 19:49

Revisions

  1. lightman76 revised this gist Feb 16, 2013. 1 changed file with 42 additions and 36 deletions.
    78 changes: 42 additions & 36 deletions backbone-rivets.config.js
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,42 @@
    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) {
    return obj[keypath];
    } else {
    return obj.get(keypath);
    };
    },
    publish: function(obj, keypath, value) {
    if (obj instanceof Backbone.Collection) {
    obj[keypath] = value;
    } else {
    obj.set(keypath, value);
    };
    }
    }
    });
    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);
    }
    }
    }
    });
  2. @wulftone wulftone renamed this gist Feb 11, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @wulftone wulftone created this gist Feb 11, 2013.
    36 changes: 36 additions & 0 deletions rivets.config.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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) {
    return obj[keypath];
    } else {
    return obj.get(keypath);
    };
    },
    publish: function(obj, keypath, value) {
    if (obj instanceof Backbone.Collection) {
    obj[keypath] = value;
    } else {
    obj.set(keypath, value);
    };
    }
    }
    });