mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-08 03:00:26 -04:00
Merge branch 'master' of git://github.com/documentcloud/backbone
This commit is contained in:
32
backbone.js
32
backbone.js
@@ -28,11 +28,11 @@
|
||||
Backbone.VERSION = '0.3.3';
|
||||
|
||||
// Require Underscore, if we're on the server, and it's not already present.
|
||||
var _ = root._ || root.$;
|
||||
var _ = root._;
|
||||
if (!_ && (typeof require !== 'undefined')) _ = require('underscore')._;
|
||||
|
||||
// For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable.
|
||||
var $ = root.jQuery || root.Zepto || root.$;
|
||||
// For Backbone's purposes, jQuery or Zepto owns the `$` variable.
|
||||
var $ = root.jQuery || root.Zepto;
|
||||
|
||||
// Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
|
||||
// to its previous owner. Returns a reference to this Backbone object.
|
||||
@@ -274,8 +274,8 @@
|
||||
options || (options = {});
|
||||
var model = this;
|
||||
var success = options.success;
|
||||
options.success = function(resp) {
|
||||
if (!model.set(model.parse(resp), options)) return false;
|
||||
options.success = function(resp, status, xhr) {
|
||||
if (!model.set(model.parse(resp, xhr), options)) return false;
|
||||
if (success) success(model, resp);
|
||||
};
|
||||
options.error = wrapError(options.error, model, options);
|
||||
@@ -290,9 +290,9 @@
|
||||
if (attrs && !this.set(attrs, options)) return false;
|
||||
var model = this;
|
||||
var success = options.success;
|
||||
options.success = function(resp) {
|
||||
if (!model.set(model.parse(resp), options)) return false;
|
||||
if (success) success(model, resp);
|
||||
options.success = function(resp, status, xhr) {
|
||||
if (!model.set(model.parse(resp, xhr), options)) return false;
|
||||
if (success) success(model, resp, xhr);
|
||||
};
|
||||
options.error = wrapError(options.error, model, options);
|
||||
var method = this.isNew() ? 'create' : 'update';
|
||||
@@ -324,7 +324,7 @@
|
||||
|
||||
// **parse** converts a response into the hash of attributes to be `set` on
|
||||
// the model. The default implementation is just to pass the response along.
|
||||
parse : function(resp) {
|
||||
parse : function(resp, xhr) {
|
||||
return resp;
|
||||
},
|
||||
|
||||
@@ -514,8 +514,8 @@
|
||||
options || (options = {});
|
||||
var collection = this;
|
||||
var success = options.success;
|
||||
options.success = function(resp) {
|
||||
collection[options.add ? 'add' : 'refresh'](collection.parse(resp), options);
|
||||
options.success = function(resp, status, xhr) {
|
||||
collection[options.add ? 'add' : 'refresh'](collection.parse(resp, xhr), options);
|
||||
if (success) success(collection, resp);
|
||||
};
|
||||
options.error = wrapError(options.error, collection, options);
|
||||
@@ -530,14 +530,14 @@
|
||||
if (!(model instanceof Backbone.Model)) {
|
||||
var attrs = model;
|
||||
model = new this.model(null, {collection: coll});
|
||||
if (!model.set(attrs)) return false;
|
||||
if (!model.set(attrs, options)) return false;
|
||||
} else {
|
||||
model.collection = coll;
|
||||
}
|
||||
var success = options.success;
|
||||
options.success = function(nextModel, resp) {
|
||||
options.success = function(nextModel, resp, xhr) {
|
||||
coll.add(nextModel);
|
||||
if (success) success(nextModel, resp);
|
||||
if (success) success(nextModel, resp, xhr);
|
||||
};
|
||||
model.save(null, options);
|
||||
return model;
|
||||
@@ -545,7 +545,7 @@
|
||||
|
||||
// **parse** converts a response into a list of models to be added to the
|
||||
// collection. The default implementation is just to pass it through.
|
||||
parse : function(resp) {
|
||||
parse : function(resp, xhr) {
|
||||
return resp;
|
||||
},
|
||||
|
||||
@@ -730,7 +730,7 @@
|
||||
};
|
||||
|
||||
// Cached regex for cleaning hashes.
|
||||
var hashStrip = /^#*/;
|
||||
var hashStrip = /^#*!?/;
|
||||
|
||||
// Cached regex for detecting MSIE.
|
||||
var isExplorer = /msie [\w.]+/;
|
||||
|
||||
@@ -1376,6 +1376,8 @@ Accounts.fetch();
|
||||
the model, or <tt>false</tt> if a validation error prevented the
|
||||
model from being created. In order for this to work, your should set the
|
||||
<a href="#Collection-model">model</a> property of the collection.
|
||||
The <b>create</b> method can accept either an attributes hash or an
|
||||
existing, unsaved model object.
|
||||
</p>
|
||||
|
||||
<pre>
|
||||
@@ -1547,6 +1549,12 @@ openPage: function(pageNumber) {
|
||||
and all of the routes are set up properly, call <tt>Backbone.history.start()</tt>
|
||||
to begin monitoring <tt>hashchange</tt> events, and dispatching routes.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
When called, if a route succeeds with a match for the current URL,
|
||||
<tt>Backbone.history.start()</tt> returns <tt>true</tt>. If no defined
|
||||
routes matches the current URL fragment, it returns <tt>false</tt>.
|
||||
</p>
|
||||
|
||||
<pre>
|
||||
$(function(){
|
||||
|
||||
@@ -233,6 +233,22 @@ $(document).ready(function() {
|
||||
equals(col.create({"foo":"bar"}),false);
|
||||
});
|
||||
|
||||
test("Collection: a failing create runs the error callback", function() {
|
||||
var ValidatingModel = Backbone.Model.extend({
|
||||
validate: function(attrs) {
|
||||
return "fail";
|
||||
}
|
||||
});
|
||||
var ValidatingCollection = Backbone.Collection.extend({
|
||||
model: ValidatingModel
|
||||
});
|
||||
var flag = false;
|
||||
var callback = function(model, error) { flag = true; };
|
||||
var col = new ValidatingCollection();
|
||||
col.create({"foo":"bar"}, { error: callback });
|
||||
equals(flag, true);
|
||||
});
|
||||
|
||||
test("collection: initialize", function() {
|
||||
var Collection = Backbone.Collection.extend({
|
||||
initialize: function() {
|
||||
|
||||
@@ -106,4 +106,13 @@ $(document).ready(function() {
|
||||
}, 10);
|
||||
});
|
||||
|
||||
asyncTest("Controller: routes (hashbang)", 2, function() {
|
||||
window.location.hash = '!search/news';
|
||||
setTimeout(function() {
|
||||
equals(controller.query, 'news');
|
||||
equals(controller.page, undefined);
|
||||
start();
|
||||
}, 10);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user