Skip to content

Commit

Permalink
Merge pull request #18 from linn/always-follow
Browse files Browse the repository at this point in the history
Now always trigger a follow event Fixes #17
  • Loading branch information
liddellj committed Jul 23, 2014
2 parents d54444e + e565c05 commit 9a6aea5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 44 deletions.
8 changes: 4 additions & 4 deletions spec/Backbone.HypermediaModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ define(function (require) {
expect(Backbone.sync.argsForCall[0][1]).toBe(sut);
});

it('should not fire a follow event', function () {
expect(followFired).toBe(false);
it('should fire a follow event', function () {
expect(followFired).toBe(true);
});
});

Expand Down Expand Up @@ -170,8 +170,8 @@ define(function (require) {
expect(Backbone.sync.argsForCall[0][1]).toBe(sut);
});

it('should not fire a follow event', function () {
expect(followFired).toBe(false);
it('should fire a follow event', function () {
expect(followFired).toBe(true);
});
});

Expand Down
77 changes: 37 additions & 40 deletions src/backbone-hypermedia-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,56 @@
} (function (Backbone, _, $) {
Backbone.HypermediaModel = Backbone.Model.extend({
follow: function (relsToFollow) {
if (!this.links) {
return;
}

var keys = _.keys(this.links);
var promises = [];
var self = this;

for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var context;
var links;

if (!relsToFollow || _.contains(relsToFollow, key)) {
if (key.split('.').length > 1) {
var item;
var attr = key.split('.')[0];
key = key.split('.')[1];
context = this.get(attr);

if (context instanceof Array) {
for (var a = 0; a < context.length; a++) {
item = context[a];
links = item.links;
addPromises(promises, links, model(self.links[keys[i]]), item, key, options(self.links[keys[i]]));
}
if (this.links) {
var keys = _.keys(this.links);

continue;
} else if (context instanceof Backbone.Collection) {
for (var b = 0; b < context.length; b++) {
item = context[b];
links = item.get('links');
addPromises(promises, links, model(self.links[keys[i]]), item, key, options(self.links[keys[i]]));
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var context;
var links;

if (!relsToFollow || _.contains(relsToFollow, key)) {
if (key.split('.').length > 1) {
var item;
var attr = key.split('.')[0];
key = key.split('.')[1];
context = this.get(attr);

if (context instanceof Array) {
for (var a = 0; a < context.length; a++) {
item = context[a];
links = item.links;
addPromises(promises, links, model(self.links[keys[i]]), item, key, options(self.links[keys[i]]));
}

continue;
} else if (context instanceof Backbone.Collection) {
for (var b = 0; b < context.length; b++) {
item = context[b];
links = item.get('links');
addPromises(promises, links, model(self.links[keys[i]]), item, key, options(self.links[keys[i]]));
}
} else if (context instanceof Backbone.Model) {
links = context.get('links');
} else {
links = context.links;
}
} else if (context instanceof Backbone.Model) {
links = context.get('links');
} else {
links = context.links;
context = this;
links = this.get('links');
}
} else {
context = this;
links = this.get('links');
}

addPromises(promises, links, model(this.links[keys[i]]), context, key, options(this.links[keys[i]]));
addPromises(promises, links, model(this.links[keys[i]]), context, key, options(this.links[keys[i]]));
}
}
}

return $.when.apply($, promises)
.then(function () {
if (promises.length > 0) {
self.trigger('follow', self);
}
self.trigger('follow', self);
});
},

Expand Down

0 comments on commit 9a6aea5

Please sign in to comment.