Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move shouldFetch to model and collection instances

  • Loading branch information...
commit b40b854aa57c6d74e703cfcaeb92debdd0c395df 1 parent b1526bb
Ryan Eastridge authored
View
3  src/collection.js
@@ -19,6 +19,9 @@ Thorax.Collection = Backbone.Collection.extend({
isPopulated: function() {
return this._fetched || this.length > 0 || (!this.length && !_.result(this, 'url'));
},
+ shouldFetch: function(options) {
+ return options.fetch && !!_.result(this, 'url') && !this.isPopulated();
+ },
fetch: function(options) {
options = options || {};
var success = options.success;
View
34 src/data-object.js
@@ -68,12 +68,15 @@ _.extend(Thorax.View.prototype, {
bindEvents.call(this, type, dataObject, this.constructor);
bindEvents.call(this, type, dataObject, this);
- if (Thorax.Util.shouldFetch(dataObject, options)) {
- loadObject(dataObject, options);
- } else if (inheritVars[type].change) {
- // want to trigger built in rendering without triggering event on model
- inheritVars[type].change.call(this, dataObject, options);
+ if (dataObject.shouldFetch) {
+ if (dataObject.shouldFetch(options)) {
+ loadObject(dataObject, options);
+ } else if (inheritVars[type].change) {
+ // want to trigger built in rendering without triggering event on model
+ inheritVars[type].change.call(this, dataObject, options);
+ }
}
+
return true;
},
@@ -141,24 +144,3 @@ function getEventCallback(callback, context) {
function ensureDataObjectCid(type, obj) {
obj.cid = obj.cid || _.uniqueId(type);
}
-
-Thorax.Util.shouldFetch = function(modelOrCollection, options) {
- if (!options.fetch) {
- return;
- }
-
- var isCollection = !modelOrCollection.collection && modelOrCollection._byCid && modelOrCollection._byId,
- url = (
- (!modelOrCollection.collection && _.result(modelOrCollection, 'urlRoot')) ||
- (modelOrCollection.collection && _.result(modelOrCollection.collection, 'url')) ||
- (isCollection && _.result(modelOrCollection, 'url'))
- );
-
- return url && !(
- (modelOrCollection.isPopulated && modelOrCollection.isPopulated()) ||
- (isCollection
- ? Thorax.Collection && Thorax.Collection.prototype.isPopulated.call(modelOrCollection)
- : Thorax.Model.prototype.isPopulated.call(modelOrCollection)
- )
- );
-};
View
11 src/model.js
@@ -17,6 +17,17 @@ Thorax.Model = Backbone.Model.extend({
}
var keys = _.keys(attributes);
return keys.length > 1 || (keys.length === 1 && keys[0] !== this.idAttribute);
+ },
+ shouldFetch: function(options) {
+ // TODO: if https://github.com/documentcloud/backbone/pull/2057 is merged
+ // then add {silent: true} to url call
+ var url;
+ try {
+ url = this.url();
+ } catch(e) {
+ url = false;
+ }
+ return options.fetch && !!url && !this.isPopulated();
}
});
View
2  test/src/form.js
@@ -64,7 +64,7 @@ describe('form', function() {
template: '<input name="parentKey">{{view child}}' + mockViewHelperFragment
});
view.render();
- var model = new Backbone.Model({
+ var model = new Thorax.Model({
parentKey: 'parentValue',
childKey: 'childValue'
});
View
2  test/src/helpers/view.js
@@ -57,7 +57,7 @@ describe('view helper', function() {
this.on('rendered', function() {
++parentRenderedCount;
});
- this.childModel = new Backbone.Model({
+ this.childModel = new Thorax.Model({
value: 'a'
});
this.child = new Thorax.Views.child({
View
29 test/src/model.js
@@ -1,23 +1,24 @@
describe('model', function() {
it("shouldFetch", function() {
- [Thorax, Backbone].forEach(function(type) {
- var options = {fetch: true};
- var a = new (type.Model.extend())();
- expect(Thorax.Util.shouldFetch(a, options)).to.not.be.ok;
+ var options = {fetch: true};
+ var a = new (Thorax.Model.extend())();
+ expect(a.shouldFetch(options)).to.not.be.ok;
- var b = new (type.Model.extend({urlRoot: '/'}))();
- expect(Thorax.Util.shouldFetch(b, options)).to.be['true'];
+ var b = new (Thorax.Model.extend({urlRoot: '/'}))();
+ expect(b.shouldFetch(options)).to.be['true'];
- var c = new (type.Model.extend({urlRoot: '/'}))();
- c.set({key: 'value'});
- expect(Thorax.Util.shouldFetch(c, options)).to.not.be.ok;
+ var c = new (Thorax.Model.extend({urlRoot: '/'}))();
+ c.set({key: 'value'});
+ expect(c.shouldFetch(options)).to.not.be.ok;
- var d = new (type.Collection.extend())();
- expect(Thorax.Util.shouldFetch(d, options)).to.not.be.ok;
+ var d = new (Thorax.Collection.extend())();
+ expect(d.shouldFetch(options)).to.not.be.ok;
- var e = new (type.Collection.extend({url: '/'}))();
- expect(Thorax.Util.shouldFetch(e, options)).to.be['true'];
- });
+ var e = new (Thorax.Collection.extend({url: '/'}))();
+ expect(e.shouldFetch(options)).to.be['true'];
+
+ var f = new (Thorax.Collection.extend({url: '/'}))();
+ expect(e.shouldFetch({fetch: false})).to.be['false'];
});
it("model view binding", function() {
Please sign in to comment.
Something went wrong with that request. Please try again.