Get current route name from corresponding loading route in ember - ember.js

As per ember document If any model hook in routes taking time to fetch data from server, ember will insert loading route. for example test-loading, at that time if we try to get current route name like this, it returns only the loading route. for example if sample route is getting too much of time to load, ember will transition into sample-loading until sample route resolved. Then again it will change to sample route. So at the time of sample-loading getCurrentRoute is returns the sample-loading. In my case I need to know the actual route name which is changed into -loading. I can't find any documentation in internet for doing this. If anyone know the way or idea to implement kindly share with me..

The pragmatic approach is following:
let currentRoute = 'sample-loading';
if (currentRoute.endsWith('-loading')) {
currentRoute = currentRoute.substring(0, currentRoute.lastIndexOf("-loading"));
}
console.log(currentRoute);

Related

Retrieve hasMany child records in route

I have the following route:
/projects/5/files
I read in the API that using find() and findAll() will automatically update any templates that use their results when records are pushed to the store. How can I do that with the child file records?
Take note that the file records are already in the store as they are returned embedded with the project response from the server.
Currently, this is the model hook in my projects/files.js route:
model() {
return this.modelFor('project').get('attachments')
}
This works on first load but won't update when adding records to the store afterwards.
Actually it does update!
Checkout this ember-twiddle!
You can not just add an attachment but also have to connect it to the project!

Sails API (Ember frontend) rejecting changes

I'm getting started with Ember (CLI) and am trying to create a simple CRUD setup, using Sails as my API. Ember's loading all the data as expected, but won't save any changes. I've made some actions buttons to toggle Booleans, and increment a counter, but these just revert back. However, Sails' default attribute "updatedAt" is updated with the date of the attempted update.
It seems GET and DELETE work fine, but am I perhaps missing a step for PUT and POST to work properly?
I'm using the mphasize's sails-ember-blueprints, but haven't read about much trouble with them.
here
It turns out that I had some issues with plurality. The model title in the json payload was singular, and the backend plural. Took a little rejigging, but this was only a simple test project.

Are there any good references for Request Lifecycle Hooks in Ember?

I recently had a bug wherein a piece of code I had written in a route's afterModel hook was not working because I needed reference to the routes controller which is not available at that time. This got me started looking for resources to understand the ordering and details of the hooks present in various pieces of Ember.
The only good resource I found was a single blogpost: Router Request Lifecycle which lists:
- enter (private)
- activate - executed when entering the route
- deserialize (private)
- model (formely deserialize) - takes the params and returns a model which is set to the route’s currentModel
- serialize - used to generate dynamic segments in the URL from a model
- setupController - takes currentModel and sets it to the controller’s content by default
- renderTemplate - takes current controller and what model returns and renders the template
with an appropriate name
deactivate - executed when exiting the route (called by exit internally)
exit (private, requires call to this._super)
While all of these hooks are mentioned in the guides disparately, there is no organized resource in the official guides which lays out the various hooks in chronological order and explains the blog post does. Please do not just say "Read the guides"
So I'm interested to see if there are any other resources which display the chronological order of how hooks are called on view, model, controller or template setup.
Thanks to #teddyzeenny here's a jsbin that shows what fires in what order on the model:
"before model"
"model"
"after model"
"activate"
"setup controller"

Making a Server Request Every Time an Ember Route is entered

When an ember route is entered with a dynamic path, ember data will load the object preloaded in the store if it exists and not make a server request. For example:
App.SomethingRoute=Ember.route.extend({
model:function(params){
this.store.find("something",params.something_id)
}
})
My app is such that I don't want to perform updating of depend models server side(I will for simple relationships but there are other I want to just pull updated records from the server). So I have been able to solve the problem by incorporating a server request in the afterModel hook:
App.SomethingRoute=Ember.route.extend({
model:function(params){
this.store.find("something",params.something_id)
},
afterModel:function(model){
$.getJSON("/somethings/"+model.id).then(function(data){
var serialized_something=route.store.serializerFor("something").normalize(TaxProgram.Something,data.something)
route.store.update("something",serialized_something)
})
})
What I can't figure out is how to check to see if the model hook is actually called, and in that case not make an additional wasteful afterModel call. I could set a property on the route that contains this information but I was hoping that Ember had a method to do this. Any suggestions?
No, there is no specific provision in Ember to handle the situation you describe.
In a similar situation I did exactly what you said you want to avoid, which is to set a property to remember if the model hook had been called. beforeModel is a useful place to initialize that property.
However, your implementation of this notion is flawed, and you're replicating too much Ember Data logic in your afterModel hook. Instead, you should simply consider using unload to remove the model instance from the local store when unnecessary and force a refresh next time you do a find on it, or do a reload at the appropriate point to force the reload.

How to use Ember Adapter

Why the 'Todos.ApplicationAdapter = DS.FixtureAdapter.extend();' replace to '
Todos.ApplicationAdapter = DS.LSAdapter.extend({
namespace: "todos-emberjs"
});
' can be achieved local stores?
what's the meaning of 'namespace: "todos-emberjs"'?
There are how much kinds of adapters? And I should how to use them? How to define an adapter?
(Check out the picture here to see where ADAPTER component fits in)
I just went through EmberJS tutorial recently and from what I understood:
1)What are EmberJS adapters?
The adapters are objects that take care of communication between your application and a server. Whenever your application asks the store for a record that it doesn't have cached, it will ask the adapter for it. If you change a record and save it, the store will hand the record to the adapter to send the appropriate data to your server and confirm that the save was successful.
2)What types of EmberJS adapters are available?
Right now I am only aware of DS.RESTAdapter which is used by default by the store (it communicates with an HTTP server by transmitting JSON via XHR), DS.FixtureAdapter(something like in-memory storage which is not persistent) and DS.LSAdapter(something like local-storage which is persistent).
3)Why LSAdapter instead of FixtureAdapter in Todos tutorial?
FixtureAdapter stores data in-memory and thus whenever you refresh your page, the data gets reassigned to initial values. But LSAdapter is available on github which uses persistent storage to store and retrieve data, hence enabling you to retain all the changes even after you refresh your page.
4)Why namespace: "todos-emberjs"?
If your JSON API lives somewhere other than on the host root, you can set a prefix that will be added to all requests. For example, if your JSON APIs are available at /todo-emberjs/ you would want it to be used as a prefix to all the URLs that you are going to call. In that case, set namespace property to todo-emberjs.
(Hope it helps, loving EmberJS btw !)