Skip to content

Commit

Permalink
patching Model and Collection directly
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterloftis committed May 12, 2012
1 parent c32bd9d commit 79d58b4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
24 changes: 24 additions & 0 deletions backbone.viewcollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(function(Backbone) {

// The original Backbone.Collection that we're extending
var proto = Backbone.Collection.prototype;

Backbone.ViewCollection = Backbone.Collection.extend({
// TODO: ViewCollection methods
});

// Add tracking to Backbone.Collection
function extendCollection(name) {
Backbone.ViewCollection.prototype[name] = function() {
// Trigger an update on any write operation...
Backbone.Virtual.track(this, 'add remove reset change create sort');
return proto[name].apply(this, arguments);
};
}
// ...if a Virtual function performs a Collection read operation:
_.each(['get', 'getByCid', 'where', 'pluck', 'clone', 'at', 'toJSON'], extendCollection);

// Alias back to Collection
Backbone.Collection.prototype = Backbone.ViewCollection.prototype;

})(Backbone);
7 changes: 6 additions & 1 deletion backbone.viewmodel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(function(Backbone) {

var proto = Backbone.Model.prototype;

Backbone.ViewModel = Backbone.Model.extend({

initialize: function(attributes, options) {
Expand All @@ -19,7 +21,7 @@
return virtual.set.call(this, key, value, options, virtual);
}
}
return Backbone.Model.prototype.set.apply(this, arguments);
return proto.set.apply(this, arguments);
},

bindView: function(attribute, container) {
Expand Down Expand Up @@ -156,4 +158,7 @@

});

Backbone.Model.prototype = Backbone.ViewModel.prototype;


})(Backbone);
19 changes: 0 additions & 19 deletions backbone.virtual.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
(function(Backbone) {

// Add tracking to Backbone.Model
Backbone.Model.prototype.get = function(attr) {
Backbone.Virtual.track(this, 'change:' + attr);
return this.attributes[attr];
};

// Add tracking to Backbone.Collection
function extendCollection(name) {
Backbone.Collection.prototype['_' + name] = Backbone.Collection.prototype[name];
Backbone.Collection.prototype[name] = function() {
// Trigger an update on any write operation...
Backbone.Virtual.track(this, 'add remove reset change create sort');
return this['_' + name].apply(this, arguments);
};
}
// ...if a Virtual function performs a Collection read operation:
_.each(['get', 'getByCid', 'where', 'pluck', 'clone', 'at', 'toJSON'], extendCollection);


// Options = model, attr, get, set, fail
Backbone.Virtual = function(options) {
_.extend(this, options);
Expand Down
8 changes: 4 additions & 4 deletions test/each.qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ $(document).ready(function() {

var vm = new Backbone.ViewModel({
listItems: new Backbone.Collection([
new Backbone.ViewModel({ label: 'a' }),
new Backbone.ViewModel({ label: 'b' }),
new Backbone.ViewModel({ label: 'c' }),
new Backbone.ViewModel({ label: 'd' })
{ label: 'a' },
{ label: 'b' },
{ label: 'c' },
{ label: 'd' }
])
});

Expand Down
1 change: 1 addition & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<!-- Backbone.ViewModel -->
<script src='../backbone.viewmodel.js'></script>
<script src='../backbone.viewcollection.js'></script>
<script src='../backbone.virtual.js'></script>
<script src='../backbone.binding.js'></script>
<script src='../lib/bindings.js'></script>
Expand Down

0 comments on commit 79d58b4

Please sign in to comment.