Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Table#findOrFetch passes a boolean to callbacks indicating whether it…

… fetched or not.
  • Loading branch information...
commit a495423fa92aa1864069fa003ad0db9701e89b4b 1 parent 25fe94c
Nathan Sobo authored
41 lib/monarch/http/ajax_future.js
View
@@ -34,17 +34,22 @@ _.constructor("Monarch.Http.AjaxFuture", {
}
},
- triggerSuccess: function(data) {
+ triggerSuccess: function(/*data...*/) {
+ var data = _.toArray(arguments);
+
this.triggered = true;
this.successful = true;
this.data = data;
- this.beforeEventsNode.publish(data);
- this.afterEventsNode.publish(data);
- this.onSuccessNode.publish(data);
- this.onCompleteNode.publish(data);
+ this.beforeEventsNode.publishArgs(data);
+ this.afterEventsNode.publishArgs(data);
+ this.onSuccessNode.publishArgs(data);
+ this.onCompleteNode.publishArgs(data);
},
- updateRepositoryAndTriggerCallbacks: function(data, repositoryOperation) {
+ updateRepositoryAndTriggerCallbacks: function(/* data..., repositoryOperation*/) {
+ var data = _.toArray(arguments)
+ var repositoryOperation = data.pop();
+
this.triggered = true;
this.repositoryUpdated = true;
this.successful = true;
@@ -52,19 +57,21 @@ _.constructor("Monarch.Http.AjaxFuture", {
Repository.pauseEvents();
repositoryOperation();
- this.beforeEventsNode.publish(data);
+ this.beforeEventsNode.publishArgs(data);
Repository.resumeEvents();
- this.afterEventsNode.publish(data);
- this.onSuccessNode.publish(data);
- this.onCompleteNode.publish(data);
+ this.afterEventsNode.publishArgs(data);
+ this.onSuccessNode.publishArgs(data);
+ this.onCompleteNode.publishArgs(data);
},
- triggerFailure: function(data) {
+ triggerFailure: function(/*data...*/) {
+ var data = _.toArray(arguments);
+
this.triggered = true;
this.failure = true;
this.data = data;
- this.onFailureNode.publish(data);
- this.onCompleteNode.publish(data);
+ this.onFailureNode.publishArgs(data);
+ this.onCompleteNode.publishArgs(data);
},
triggerError: function(xhr, status, errorThrown) {
@@ -76,7 +83,7 @@ _.constructor("Monarch.Http.AjaxFuture", {
onSuccess: function(callback, context) {
if (this.triggered) {
- if (this.successful) callback.call(context, this.data);
+ if (this.successful) callback.apply(context, this.data);
} else {
this.onSuccessNode.subscribe(callback, context);
}
@@ -85,16 +92,16 @@ _.constructor("Monarch.Http.AjaxFuture", {
onFailure: function(callback, context) {
if (this.triggered) {
- if (this.failure) callback.call(context, this.data);
+ if (this.failure) callback.apply(context, this.data);
} else {
this.onFailureNode.subscribe(callback, context);
}
return this;
},
- onError: function(errorCallback) {
+ onError: function(errorCallback, context) {
if (this.triggered) {
- if (this.error) errorCallback.apply(null, this.data);
+ if (this.error) errorCallback.apply(context, this.data);
} else {
this.onErrorNode.subscribe(errorCallback);
}
4 lib/monarch/model/relations/table.js
View
@@ -83,12 +83,12 @@ _.constructor("Monarch.Model.Relations.Table", Monarch.Model.Relations.Relation,
var future = new Monarch.Http.AjaxFuture();
var record = this.find(id);
if (record) {
- future.triggerSuccess(record);
+ future.triggerSuccess(record, false);
} else {
var relationsToFetch = [this.where({id: id})];
if (additionalRelations) relationsToFetch = relationsToFetch.concat(additionalRelations);
Server.fetch(relationsToFetch).onSuccess(function() {
- future.triggerSuccess(this.find(id));
+ future.triggerSuccess(this.find(id), true);
}, this);
}
return future;
4 lib/monarch/subscription_node.js
View
@@ -46,6 +46,10 @@ _.constructor("Monarch.SubscriptionNode", {
}
},
+ publishArgs: function(array) {
+ this.publish.apply(this, array);
+ },
+
empty: function() {
return this.subscriptions.length == 0;
},
8 spec/monarch/model/relations/table_spec.js
View
@@ -80,15 +80,14 @@ Screw.Unit(function(c) { with(c) {
describe("#findOrFetch(id, additionalRelations)", function() {
useFakeServer(false);
- it("looks for a record in with the given id, and fetches it and any additional relations if it is not found, invoking the callback with the record", function() {
+ it("looks for a record in with the given id, and fetches it and any additional relations if it is not found, invoking the callback with the record and a boolean value indicating whether a fetch occurred", function() {
// case where a record with given id is in the repo
var extantRecord = Blog.createFromRemote({id: 1});
var onSuccessCallback = mockFunction("onSuccessCallback");
Blog.findOrFetch(1).onSuccess(onSuccessCallback);
- expect(onSuccessCallback).to(haveBeenCalled, once);
- expect(onSuccessCallback).to(haveBeenCalled, withArgs(extantRecord));
+ expect(onSuccessCallback).to(haveBeenCalled, withArgs(extantRecord, false));
expect(Server.fetches).to(beEmpty);
// case where a record with that id is not in the local repo
@@ -107,8 +106,7 @@ Screw.Unit(function(c) { with(c) {
}
});
- expect(onSuccessCallback).to(haveBeenCalled, once);
- expect(onSuccessCallback).to(haveBeenCalled, withArgs(Blog.find('on-server')));
+ expect(onSuccessCallback).to(haveBeenCalled, withArgs(Blog.find('on-server'), true));
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.