问题
I can't seem to track down the source of this error:
Assertion failed: No model was found for '0'
The JSON is getting fetched by the server, but the app is erroring out before it gets sent to the template. The problem seems to be happening between the REST adapter and the router. The template renders error-free when I use the fixture adapter.
I'm using Ember and Handlebars versions 1.0.0.
Here's my App code:
window.App = Ember.Application.create();
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:3000'
});
App.Router.map(function() {
this.resource("stories", { path: "/" }, function() {
this.resource("boards", { path: "/boards"} )
});
});
App.StoriesRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('story');
}
});
attr = DS.attr;
App.Story = DS.Model.extend({
color: attr()
});
Handlebars templates
<script type="text/x-handlebars">
{{ outlet }}
</script>
<script type="text/x-handlebars" data-template-name="stories">
<ul>
<li class="storyLine">
<ul>
<li id="colorSwatch"></li>
<li class="board">+</li>
</ul>
</li>
</ul>
<ul>
{{#each model}}
<li class="storyLine">
<ul>
<li id="colorSwatch" {{bindAttr class=story.color}}></li>
<li class="board">dddd</li>
</ul>
</li>
{{/each}}
</ul>
</script>
Thanks for your help!
回答1:
The response from the server probably isn't formatted correctly. The JSON needs to have a root that is the same name as your model. See: http://emberjs.com/guides/models/the-rest-adapter/#toc_json-root Your response needs to look something like this:
{
"story": [
{
"id": 0,
"title": "foo"
},
{
"id": 1,
"title": "bar"
}
]
}
回答2:
In your bindAttr
you are using the object story
therefore you should change your {{#each}}
helper to make reference to that object:
...
{{#each story in model}}
<li class="storyLine">
<ul>
<li id="colorSwatch" {{bindAttr class=story.color}}></li>
<li class="board">dddd</li>
</ul>
</li>
{{/each}}
...
Hope it helps.
回答3:
I got a similar error when shifting from an extended FixtureAdapter
to an extended RESTAdapter
. I was overwriting one of the finder
- methods.
While there the FixtureAdapter
was totally happy with an array of objects
findQuery: function(store, type, query, array) {
return DS.PromiseArray.create({
promise: new Promise(function(resolve, reject){
var results = [];
store.findAll('source').then(function(sources) {
sources.forEach(
function (rootSource) {
results.push( store.createRecord( ... ));
}
);
resolve(results);
},reject);
})
});
}
for the same method the RESTAdapter
expected an object with key and array as value
findQuery: function(store, type, query, array) {
return new Ember.RSVP.Promise(function(resolve, reject){
var results = [];
store.find('source').then(function(sources) {
sources.forEach(
function (rootSource) {
results.push({ ... });
}
);
resolve({"connections":results});
},reject);
});
},
See the difference within the call of resolve
.
Even it works to put an object into the array like in the FixtureAdapter
, this will result in the error
Assertion failed: You must use Ember.set() to access this property
later on when you try to change the objects properties.
For all of this, I'm using Ember 1.2.0, Ember Data : 1.0.0-beta.7+canary.b45e23ba.
Hope this helps...
来源:https://stackoverflow.com/questions/18644683/ember-1-0-0-restadapter-failure