Fixes #1363, fixes #1461, removes View#dispose, adds Events#listenTo and Events#stopListening, and implements View#remove in terms of those.

This commit is contained in:
Jeremy Ashkenas
2012-12-11 13:41:39 -05:00
parent d3afe80671
commit 1191640d84
4 changed files with 57 additions and 46 deletions

View File

@@ -840,14 +840,14 @@ $(document).ready(function() {
new Collection().push({id: 1});
});
test("`update` with non-normal id", function() {
var Collection = Backbone.Collection.extend({
model: Backbone.Model.extend({idAttribute: '_id'})
});
var collection = new Collection({_id: 1});
collection.update([{_id: 1, a: 1}], {add: false});
equal(collection.first().get('a'), 1);
});
// test("`update` with non-normal id", function() {
// var Collection = Backbone.Collection.extend({
// model: Backbone.Model.extend({idAttribute: '_id'})
// });
// var collection = new Collection({_id: 1});
// collection.update([{_id: 1, a: 1}], {add: false});
// equal(collection.first().get('a'), 1);
// });
test("#1894 - `sort` can optionally be turned off", 0, function() {
var Collection = Backbone.Collection.extend({

View File

@@ -66,6 +66,15 @@ $(document).ready(function() {
equal(obj.counter, 5);
});
test("listenTo and stopListening", 1, function() {
var a = _.extend({}, Backbone.Events);
var b = _.extend({}, Backbone.Events);
a.listenTo(b, 'all', function(){ ok(true); });
b.trigger('anything');
a.listenTo(b, 'all', function(){ ok(false); });
a.stopListening();
b.trigger('anything');
});
test("trigger all for each event", 3, function() {
var a, b, obj = { counter: 0 };
@@ -229,7 +238,7 @@ $(document).ready(function() {
var obj = { counterA: 0, counterB: 0 };
_.extend(obj,Backbone.Events);
var incrA = function(){ obj.counterA += 1; obj.trigger('event'); };
var incrB = function(){ obj.counterB += 1 };
var incrB = function(){ obj.counterB += 1; };
obj.once('event', incrA);
obj.once('event', incrB);
obj.trigger('event');

View File

@@ -41,7 +41,7 @@ $(document).ready(function() {
var div = view.make('div', {id: 'test-div'}, 0);
equal($(div).text(), '0');
var div = view.make('div', {id: 'test-div'}, '');
div = view.make('div', {id: 'test-div'}, '');
equal($(div).text(), '');
});
@@ -171,7 +171,7 @@ $(document).ready(function() {
return {
title: 'title1',
acceptText: 'confirm'
}
};
}
});
@@ -310,14 +310,11 @@ $(document).ready(function() {
ok(new View().$el.is('p'));
});
test("dispose", 0, function() {
test("views stopListening", 0, function() {
var View = Backbone.View.extend({
events: {
click: function() { ok(false); }
},
initialize: function() {
this.model.on('all x', function(){ ok(false); }, this);
this.collection.on('all x', function(){ ok(false); }, this);
this.listenTo(this.model, 'all x', function(){ ok(false); }, this);
this.listenTo(this.collection, 'all x', function(){ ok(false); }, this);
}
});
@@ -326,22 +323,9 @@ $(document).ready(function() {
collection: new Backbone.Collection
});
view.dispose();
view.stopListening();
view.model.trigger('x');
view.collection.trigger('x');
view.$el.click();
});
test("dispose with non Backbone objects", 0, function() {
var view = new Backbone.View({model: {}, collection: {}});
view.dispose();
});
test("view#remove calls dispose.", 1, function() {
var view = new Backbone.View();
view.dispose = function() { ok(true); };
view.remove();
});
test("Provide function for el.", 1, function() {
@@ -364,16 +348,16 @@ $(document).ready(function() {
counter++;
}
});
var view = new View({events:{'click #test':'increment'}});
var view2 = new View({events:function(){
return {'click #test':'increment'};
}});
view.$('#test').trigger('click');
view2.$('#test').trigger('click');
equal(counter, 2);
view.$('#test').trigger('click');
view2.$('#test').trigger('click');
equal(counter, 4);