Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Make DS.Model reload-able #546

Closed
wants to merge 2 commits into from

2 participants

@darthdeus
Collaborator

This is a first step of making models able to expire and be updated from the server, as mentioned in #545

I'm mostly looking for a feedback, since this is my first contribution to ember. I don't want to merge this yet.

darthdeus added some commits
@darthdeus darthdeus Add .reload() to DS.Model
This is a first step of making models able to expire
and be updated from the server.
60e8c9d
@darthdeus darthdeus Add tests for DS.Model#reload() ae67630
@darthdeus
Collaborator

Just now I realized the test never fails ... in my manual testing I was using RESTAdapter and updaing the data server side, and using adapter.load to force a new request which reloads the data. I'm not sure how to simulate that in the test thought.

@darthdeus
Collaborator

After a few days spent digging around the source code, I realized that I probably won't be able to implement the whole #545 TTL thing.

On the other hand, I found the reload() method very useful in my development, so I'd like to merge this.

If someone could provide some feedback that would be great :)

@tchak
Collaborator

The reloading was discussed a few times. One of the concerns is data merging. What should happen if there is conflicts between current client state and server state? There is some work planed on data merging facility, we could expect reloading to land at the same moment.

@darthdeus
Collaborator

@tchak If you're intentionally reloading, then it should replace all of the data stored on the client side, because you are basically saying "I want the fresh copy of XYZ".

For example if you have a news block in a footer, it's ok to just reload the data and throw the old copy away, because the only reason why you're doing it is to get the new data.

@tchak
Collaborator

This is what is happening currently. Except that at the time you reloading a user can be editing your record, what will you do in this case? And there is a use case for partial data loading.

@darthdeus
Collaborator

Yes that is tricky, but you can either chose not to reload when a user is editing, or just reload in the case when this just can't happen.

This reload() is supposed to be only for read only data which changes often or externaly. Nothing more than a syntactic sugar.

@tchak
Collaborator

@darthdeus If all you want is some "sugar", your current solution is juste fine. Most of us, have a similar implementation in our app. But for inclusion in core, a reload mechanism must have all the bells and whistles. We can not juste throw in a solution that will work in "some" cases. Most developers do not know, do not want to know and should not have to know what they are doing :)

We also should have a way to mark the record as reloading.

@darthdeus
Collaborator

@tchak Ok let's close this and keep the discussion in the main issue #545. It is still an issue, but this implementation isn't helping.

@darthdeus darthdeus closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 22, 2012
  1. @darthdeus

    Add .reload() to DS.Model

    darthdeus authored
    This is a first step of making models able to expire
    and be updated from the server.
  2. @darthdeus
This page is out of date. Refresh to see the latest.
View
12 packages/ember-data/lib/system/model/model.js
@@ -37,6 +37,18 @@ DS.Model = Ember.Object.extend(Ember.Evented, {
return store.serialize(this, options);
},
+ /*
+ Reload the record's data, bypassing the local cached copy
+ and fetching everything from the server again.
+ */
+ reload: function() {
+ var store = get(this, 'store'),
+ adapter = store.get('adapter');
+
+ Ember.assert("You can only reload with a proper adapter", Ember.typeOf(adapter.find) === "function");
+ adapter.find(store, this.constructor, this.get("id"));
+ },
+
didLoad: Ember.K,
didUpdate: Ember.K,
didCreate: Ember.K,
View
15 packages/ember-data/tests/unit/model_test.js
@@ -9,7 +9,10 @@ var testSerializer = DS.JSONSerializer.create({
});
var TestAdapter = DS.Adapter.extend({
- serializer: testSerializer
+ serializer: testSerializer,
+ find: function(store, type, id) {
+ return store.find(type, id);
+ }
});
module("DS.Model", {
@@ -61,6 +64,16 @@ test("a record's id is included in its toString represenation", function() {
equal(record.toString(), '<(subclass of DS.Model):'+Ember.guidFor(record)+':1>', "reports id in toString");
});
+test("a record can reload itself", function() {
+ store.load(Person, { id: 1, name: "Yehuda" });
+
+ var record = store.find(Person, 1);
+ store.load(Person, { id: 1, name: "Tomhuda" });
+ record.reload();
+
+ equal(get(record, 'name'), "Tomhuda", "name was not reloaded properly");
+});
+
test("trying to set an `id` attribute should raise", function() {
Person = DS.Model.extend({
id: DS.attr('number'),
Something went wrong with that request. Please try again.