Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX release] shouldReloadRecord and shouldBackgroundReloadRecord … #3468

Merged
merged 1 commit into from
Jul 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,34 +603,16 @@ Store = Service.extend({
var internalModel = this._internalModelForId(modelName, id);
options = options || {};

return this._findByInternalModel(internalModel, options);
},

_findByInternalModel: function(internalModel, options) {
options = options || {};

if (options.preload) {
internalModel._preloadData(options.preload);
if (!this.hasRecordForId(modelName, id)) {
return this._findByInternalModel(internalModel, options);
}

var fetchedInternalModel = this._fetchOrResolveInternalModel(internalModel, options);
var fetchedInternalModel = this._findRecord(internalModel, options);

return promiseRecord(fetchedInternalModel, "DS: Store#findRecord " + internalModel.typeKey + " with id: " + get(internalModel, 'id'));
},

_fetchOrResolveInternalModel: function(internalModel, options) {
var typeClass = internalModel.type;
var adapter = this.adapterFor(typeClass.modelName);
// Always fetch the model if it is not loaded
if (internalModel.isEmpty()) {
return this.scheduleFetch(internalModel, options);
}

//TODO double check about reloading
if (internalModel.isLoading()) {
return internalModel._loadingPromise;
}

_findRecord: function(internalModel, options) {
// Refetch if the reload option is passed
if (options.reload) {
return this.scheduleFetch(internalModel, options);
Expand All @@ -639,6 +621,8 @@ Store = Service.extend({
// Refetch the record if the adapter thinks the record is stale
var snapshot = internalModel.createSnapshot();
snapshot.adapterOptions = options && options.adapterOptions;
var typeClass = internalModel.type;
var adapter = this.adapterFor(typeClass.modelName);
if (adapter.shouldReloadRecord(this, snapshot)) {
return this.scheduleFetch(internalModel, options);
}
Expand All @@ -651,6 +635,32 @@ Store = Service.extend({
// Return the cached record
return Promise.resolve(internalModel);
},

_findByInternalModel: function(internalModel, options) {
options = options || {};

if (options.preload) {
internalModel._preloadData(options.preload);
}

var fetchedInternalModel = this._findEmptyInternalModel(internalModel, options);

return promiseRecord(fetchedInternalModel, "DS: Store#findRecord " + internalModel.typeKey + " with id: " + get(internalModel, 'id'));
},

_findEmptyInternalModel: function(internalModel, options) {
if (internalModel.isEmpty()) {
return this.scheduleFetch(internalModel, options);
}

//TODO double check about reloading
if (internalModel.isLoading()) {
return internalModel._loadingPromise;
}

return Promise.resolve(internalModel);
},

/**
This method makes a series of requests to the adapter's `find` method
and returns a promise that resolves once they are all loaded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1018,3 +1018,51 @@ test("findAll should pass adapterOptions to the findAll method", function() {
store.findAll('person', { adapterOptions: { query: { embed: true } } });
});
});


test("An async hasMany relationship with links should not trigger shouldBackgroundReloadRecord", function() {
var Post = DS.Model.extend({
name: DS.attr("string"),
comments: DS.hasMany('comment', { async: true })
});

var Comment = DS.Model.extend({
name: DS.attr("string")
});

env = setupStore({
post: Post,
comment: Comment,
adapter: DS.RESTAdapter.extend({
findRecord: function() {
return {
posts: {
id: 1,
name: "Rails is omakase",
links: { comments: '/posts/1/comments' }
}
};
},
findHasMany: function() {
return Ember.RSVP.resolve({
comments: [
{ id: 1, name: "FIRST" },
{ id: 2, name: "Rails is unagi" },
{ id: 3, name: "What is omakase?" }
]
});
},
shouldBackgroundReloadRecord: function() {
ok(false, 'shouldBackgroundReloadRecord should not be called');
}
})
});

store = env.store;

run(store, 'find', 'post', '1').then(async(function(post) {
return post.get('comments');
})).then(async(function(comments) {
equal(comments.get('length'), 3);
}));
});