Calling findAll during route transition - ember.js

I'm using Ember 1.4 with EmberData beta 7. The routes in my application are fairly straight forward and looks like this. ScenarioController and StratsControllers are ArrayControllers. StratsStratController is an ObjectController.
App.Router.map(function () {
this.route('scenarios', {path: "/scenarios"});
this.resource('strats', {path: "/"}, function() {
this.route('strat', {path: "/strat/:strat_id"});
});
});
When I first transitioned into the 'strats' route, Ember calls the findAll method, which makes a request to my server for all 'strat' instances as expected. My server returns all data associated with the 'strat' model, side loading all related hasMany records. Then I transitioned to the scenarios route, and Ember calls findAll to fetch all 'scenario' instances, which was also as expected. However, when I transitioned back to the 'strats' route via the browser's back button, I see another GET message from Ember to my server requesting all 'strat' instances again. This surprised me. Why did Ember make another call to findAll for the 'strat' instances when it already has it in DS.store? Is this expected behavior?

I think its because its a live array of data, so ember checks to see if anything has changed in the mean time. You can change the data to a static array using the toArray() method but the way it is currently means that any updates to the database are automatically reflected on the client.
I have just come across this in the docs:
FINDING ALL RECORDS OF A TYPE
1
var posts = this.store.find('post'); // => GET /posts
To get a list of records already loaded into the store, without making another network request, use all instead.
1
var posts = this.store.all('post'); // => no network request
find returns a DS.PromiseArray that fulfills to a DS.RecordArray and all directly returns a DS.RecordArray.
It's important to note that DS.RecordArray is not a JavaScript array. It is an object that implements Ember.Enumerable. This is important because, for example, if you want to retrieve records by index, the [] notation will not work--you'll have to use objectAt(index) instead.

Related

What RESTAdapter expects on server responses and what requests should server expect?

I'm using Django REST Framework, not Rails (which seems to have several magical gems to make everything work swiftly with Ember) and I've been having some difficulties trying to figure out how Ember expects responses. I'm using Ember CLI, thus I'm also using Ember data.
The documentation states only the typical GET usage, when I'm simply retrieving an object or an array of objects. Documentation states:
The JSON payload should be an object that contains the record inside a root property
And about conventions:
Attribute names in your JSON payload should be the camelCased versions of the attributes in your Ember.js models.
No problem with that.
1. But how should the API respond when there are errors?
Ok, so documentation also states you could use ajaxError to check jqXHR status for an error and then return a populated DS.Error for the record. However, how should I return different kind of errors. For example, let's say the user session is now invalid and because of that the server couldn't delete a record as requested.
2. How will Ember submit requests?
I'm quite new to REST in general. I think Ember simply use the appropriate verb for the action it wants: GET, POST, PUT, DELETE. I think it's quite clear it will send all the model's field to POST a new one, but how about DELETE? Will Ember send all the record or just the ID to delete an object?
Generally you should be able to see the requests Ember makes by just opening your browser dev tools and seeing the network requests.
Ember data likes the api to respond with an errors hash, something like this:
{"errors":{"title":["can't be blank"]}}
Then as long as you define a function to handle the error case:
Ember.Controller.extend({
actions: {
deleteUser: function() {
var user = this.model;
function success() {
// do something cool?
}
function failure() {
user.rollback();
}
user.destroyRecord().then(success, failure);
}
}
});
then user.errors will be automatically populated and you can do an if user.errors in your template.

Fetching data from the store in controller

i have a getrank button which is supposed to make get request to the server
So for this i wrote a Getrank function in controller inside which i have this
var self = this;
this.store.find('post',"1").then(function(data){
self.set('mydata',data);
console.log(self.get('mydata'));
},
function(error){
alert(error);
})
It returns a class.,instead i need a json to work on
Is it a normal behaviour or am i doing something wrong?
If you need a JSON response use jQuery's getJSON method. Ember-Data gives you client side records that it manages with a plethora of additional functionality.
Ember and Ember Data are two different products and Ember works just fine without Ember-Data using POJOs

How to handle invalid routes without slash in Ember.js

At the moment, I handle invalid routes in Ember.js like this:
this.route('invalid', { path: '*path' }
This works and applies to routes like:
https://www.appname.com/#/misspelled_name
However, when using Dropbox Datastores API, I am having some problems. After an authentication request, Dropbox redirects me to:
https://www.appname.com/#access_token=...
Is there a way to handle this route? Without the slash before the route name? In this case, the 'invalid' route is not applied and I receive an error 'The route access_token=... was not found'. How should I handle this response in Ember?
UPDATE
I don't think it is possible to handle this. The only working solution for me was to do authentication before Ember was even loaded. After successful authentication, I load my ember app:
window.dropboxClient = new Dropbox.Client({
key: 'some_key'
});
dropboxClient.authenticate({ interactive: true }, function(error) {
if (error) {
return console.log('Error during authentication');
}
});
yepnope([{
test : window.dropboxClient.isAuthenticated(),
yep : ['my-ember-app.js']
}])
I don't have direct experience with EmberJS, but one possibility might be to run client.authenticate({ interactive: false }); before the EmberJS script is even loaded.
Another alternative is to specify a different redirect URI (not the same page as the rest of your app) and not load EmberJS on that page. (That page would then presumably redirect back to the main app when it was done.) You could also forgo redirects altogether and use the pop-up auth driver instead.
Before the #/, you aren't actually inside of the ember app. As a result, none of the ember code will have any impact on this.

find() and find(id) triggering Ajax calls to the server

In my application, I have the common parent/child relationship in my route map.
App.Router.map(function () {
this.resource('strats', {path: "/"}, function() {
this.route('strat', {path: "/strat/:strat_id"});
});
});
My understanding is that when Ember first enters the parent route, it calls find() to get all models, which generally triggers an Ajax call to the server. Then when Ember subsequently transitions to a child route, it first calls find(), followed by find(id). If I'm using a data layer with an identity-map implementation (such as Ember-Data or Ember-Model), these subsequent calls to find() and find(id) should result in data being fetched from local memory, and Ember should not have to initiate another Ajax call to the server as a result of calls to these functions as long as the application is running. If this understanding is correct, then I should not have to implement find(id) on the server side.
I'm using Ember-Model in my application. As I'm navigating between routes, I see on the server side requests for an individual model coming through once in awhile, which means calls to find(id) are sometimes triggering Ajax calls to the server, which was unexpected. Where's the flaw in my logic described above?
Firstly, Ember itself only does what your model hooks tell it to do.
And those hooks are only called if you are visiting the route anew. I'll give some examples of this using your router above.
App.Router.map(function () {
this.resource('cow');
this.resource('strats', {path: "/"}, function() {
this.route('strat', {path: "/strat/:strat_id"});
});
});
I visit the strats route. The model hook for that route (StratsRoute) will be hit (in your case a find, returning multiple strats).
I visit the strat/1 route. The model hook for the route (StratsStratRoute) will be hit( find(1))
I visit the strat/2 route. The model hook for the route (StratsStratRoute) will be hit( find(2))
I visit the cow route (which is outside of the strats).
I visit the strat/1 route, the model hook for each route will be called, 1 at a time down to the strat route. The StratsRoute will be called, and until that model has been returned it will wait, once it's returned it will then go to the StratsStratRoute route and wait until that's resolved, once resolved it will continue rendering the page etc.
Interestingly Ember Model has an interesting feature, you can use find or fetch. find will build up a dummy record and return it immediately, and once the ajax is returned it'll update the model. fetch will return a promise, and once the ajax has returned and the record fully built it will resolve the promise.
Note, there is a bit of a race condition in the find approach. Imagine if you do find() in the strats route, then find(1) in the strat route. They will both resolve immediately (with dummy records) and both make calls to the server. The first call will be for all of the records (which might include 1) the second call is for record #1. So we just made 2 calls for record one, that was kind of a waste. In this instance fetch would have been better. Let's think about it with fetch. fetch in the strats route, now we wait til it's resolved. Once it's resolved (aka the server returned all the models) we hit the strat route, it does fetch(1) and instead of it hitting the server, it knows it already has model 1, it came down in the strats route, so it doesn't make an ajax request, and it resolves immediately.
Hopefully this clears up the dust a bit.

How to simulate, with FIXTURES, a slow REST backend?

I am trying to simulate a slow backend in a test application using FIXTURES. I am doing the following:
App.SlowIndexRoute = Ember.Route.extend({
model: function() {
return new Ember.RSVP.Promise(function(resolve) {
Ember.run.later(function() {
resolve(App.Node.find());
}, 2000);
});
}
});
I was expecting that this would behave similarly to a slow REST backend, namely:
The request is sent
Route is activated, and the template is rendered
Reply arrives from backend
Now the data is updated in the template
Instead, this is roughly what is happening, as far as I can tell:
The request is sent
No rendering of the template is performed, the route is not yet activated.
Once the reply "arrives" (resolve(App.Node.find());) the route is activated
The template is rendered, and since the data is already available, it is also displayed
How can I more accurately simulate a slow REST backend? How can I make sure that the router is activating the view/template before the reply arrives?
DS.FixtureAdapter has a latency property that defaults to 50 milliseconds. You can change this by instantiating the adapter manually when you create your store.
App.Store = DS.Store.extend({
adapter: DS.FixtureAdapter.create({ latency: 1000 });
});
As of Ember 1.0.0-rc.6, it is expected behavior for a route to wait for the model's promise to resolve before transitioning. If you don't want the route to wait, don't return a promise (something with a then method) from your model hook. Return a regular object, one that's already loaded, or a proxy. Then load the model later, in your setupController hook, for example.