Reputation: 25135
What is the correct way to define model and routing to build a model that can be used as Application Data.
App.Router.map(function(){
this.resource("main", {path:"/"});
});
App.MainRoute = Ember.Route.extend({
model:function(){
return App.Main.find();
}
});
App.Main = DS.Model.extend({
appName:DS.attr('string')
});
App.Store = DS.Store.extend({
revision: 11,
adapter: DS.RESTAdapter.create({
namespace: 'api/v1'
})
});
This eventually invokes api/v1/mains
. But I want it to invoke api/v1/main
that returns the Application Data which is a single object like
{
"appName":"MyApp",
"lastLogin": "Sat May 11 2013 11:20:03 GMT+0530",
...
}
I know about configuring plurals, but believe that is not the right way to do it. Any help would be appreciated. Thanks in advance.
EDIT: When configuring Plurals it is not rendering template
App = Ember.Application.create();
App.Router.map(function(){
this.resource("main", {path:"/"});
});
App.MainRoute = Ember.Route.extend({
model:function(){
return App.Main.find();
}
});
DS.RESTAdapter.configure("plurals", {
main: "main"
});
App.Store = DS.Store.extend({
revision: 11,
adapter: DS.RESTAdapter.create({
namespace: 'api/v1'
})
});
App.Main = DS.Model.extend({
"appName":DS.attr('string')
});
json received from server
{
"main": {
"app_name": "My App"
}
}
template :
<script type="text/x-handlebars" data-template-name="main">
This is {{appName}}
</script>
Upvotes: 4
Views: 108
Reputation: 25135
Not sure whether this is the right approach, but solved this by invoking
App.Main.find("data");
and defining a route /api/v1/main/data
on server side.
As a result App.Main.find("data");
works like fetching single record from a list of records and an ObjectController
is generated for main
Upvotes: 0
Reputation: 106
The original issue lies in the fact that you invoke App.Main.find()
which will automatically lead to the creating of an Ember.Arraycontroller
for the content
of you MainController
(generated automatically because of your routing naming). This also invokes requesting the pluralized version of you model.
So, by modifying your plurals to point to /main, you will retrieve the info, as you show, but it is still rendered inside an Ember.ArrayController
. So to view this, you template needs to run it through an {{#each}}
. So it probably would have worked if you had templated it:
<script type="text/x-handlebars" data-template-name="main">
{{#each main}}
This is {{appName}}
{{/each}}
</script>
Your solution, identifying an individual record by parsing the key in find()
, is correct, as that (as you already correctly stated) invokes the creation of a singleton (Ember.ObjectController
) which can then be directly interrogated.
Upvotes: 1