Permalink
Browse files

Merge branch 'master' of github.com:documentcloud/backbone

  • Loading branch information...
2 parents 5ebbeb0 + fdbd3ee commit 39061ff48ae9398b1a0f03d4d48d55f20a43c6f7 @jashkenas jashkenas committed Oct 31, 2011
Showing with 3,041 additions and 1,865 deletions.
  1. +2 −1 .gitignore
  2. +6 −2 backbone.js
  3. +1 −1 examples/todos/index.html
  4. +34 −0 test/collection.js
  5. +16 −0 test/model.js
  6. +1 −1 test/test.html
  7. +2,383 −1,513 test/vendor/{jquery-1.5.js → jquery-1.6.4.js}
  8. +40 −10 test/vendor/qunit.css
  9. +558 −337 test/vendor/qunit.js
View
@@ -1 +1,2 @@
-raw
+raw
+*.sw?
View
@@ -600,6 +600,7 @@
this.models.splice(index, 0, model);
model.bind('all', this._onModelEvent);
this.length++;
+ options.index = index;
if (!options.silent) model.trigger('add', model, this, options);
return model;
},
@@ -612,8 +613,10 @@
if (!model) return null;
delete this._byId[model.id];
delete this._byCid[model.cid];
- this.models.splice(this.indexOf(model), 1);
+ var index = this.indexOf(model);
+ this.models.splice(index, 1);
this.length--;
+ options.index = index;
if (!options.silent) model.trigger('remove', model, this, options);
this._removeReference(model);
return model;
@@ -1152,7 +1155,8 @@
// Wrap an optional error callback with a fallback error event.
var wrapError = function(onError, model, options) {
- return function(resp) {
+ return function(model, resp) {
+ var resp = model === model ? resp : model;
if (onError) {
onError(model, resp, options);
} else {
@@ -5,7 +5,7 @@
<title>Backbone Demo: Todos</title>
<link href="todos.css" media="all" rel="stylesheet" type="text/css"/>
<script src="../../test/vendor/json2.js"></script>
- <script src="../../test/vendor/jquery-1.5.js"></script>
+ <script src="../../test/vendor/jquery-1.6.4.js"></script>
<script src="../../test/vendor/underscore-1.2.1.js"></script>
<script src="../../backbone.js"></script>
<script src="../backbone-localstorage.js"></script>
View
@@ -122,6 +122,23 @@ $(document).ready(function() {
equals(atCol.last(), h);
});
+ test("Collection: add model to collection and verify index updates", function() {
+ var f = new Backbone.Model({id: 20, label : 'f'});
+ var g = new Backbone.Model({id: 21, label : 'g'});
+ var h = new Backbone.Model({id: 22, label : 'h'});
+ var col = new Backbone.Collection();
+
+ var counts = [];
+
+ col.bind('add', function(model, collection, options) {
+ counts.push(options.index);
+ });
+ col.add(f);
+ col.add(g);
+ col.add(h);
+ ok(_.isEqual(counts, [0,1,2]));
+ });
+
test("Collection: add model to collection twice", function() {
try {
// no id, same cid
@@ -173,6 +190,23 @@ $(document).ready(function() {
equals(otherRemoved, null);
});
+ test("Collection: remove should return correct index events", function() {
+ var f = new Backbone.Model({id: 20, label : 'f'});
+ var g = new Backbone.Model({id: 21, label : 'g'});
+ var h = new Backbone.Model({id: 22, label : 'h'});
+ var col = new Backbone.Collection([f,g,h]);
+
+ var counts = [];
+
+ col.bind('remove', function(model, collection, options) {
+ counts.push(options.index);
+ });
+ col.remove(h);
+ col.remove(g);
+ col.remove(f);
+ ok(_.isEqual(counts, [2,1,0]));
+ });
+
test("Collection: events are unbound on remove", function() {
var counter = 0;
var dj = new Backbone.Model();
View
@@ -277,6 +277,22 @@ $(document).ready(function() {
model.set({lastName: 'Hicks'});
});
+ test("Model: validate after save", function() {
+ var lastError, model = new Backbone.Model();
+ model.validate = function(attrs) {
+ if (attrs.admin) return "Can't change admin status.";
+ };
+ model.sync = function(method, model, options) {
+ options.success.call(this, {admin: true});
+ };
+ model.save(null, {error: function(model, error) {
+ console.log('erroring!');
+ lastError = error;
+ }});
+
+ equals(lastError, "Can't change admin status.");
+ });
+
test("Model: save", function() {
doc.save({title : "Henry V"});
equals(lastRequest[0], 'update');
View
@@ -4,7 +4,7 @@
<title>Backbone Test Suite</title>
<link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="vendor/json2.js"></script>
- <script type="text/javascript" src="vendor/jquery-1.5.js"></script>
+ <script type="text/javascript" src="vendor/jquery-1.6.4.js"></script>
<script type="text/javascript" src="vendor/qunit.js"></script>
<script type="text/javascript" src="vendor/jslitmus.js"></script>
<script type="text/javascript" src="vendor/underscore-1.2.1.js"></script>
Oops, something went wrong.

0 comments on commit 39061ff

Please sign in to comment.