在Ember.js中查找记录无效

时间:2014-01-15 06:32:18

标签: ember.js

我的代码:

App = Ember.Application.create();

App.ApplicationAdapter = DS.FixtureAdapter.extend();

App.Student=DS.Model.extend({
  name:DS.attr('string'),
  projects:DS.hasMany('project',{async:true})
});

App.Project=DS.Model.extend({
  name:DS.attr('string'),
  student:DS.belongsTo('student')
});

App.Student.FIXTURES = [
 { id: 1, name: 'Kris', projects: [1,2] },
 { id: 2, name: 'Luke', projects: [3] }
];

App.Project.FIXTURES=[
 { id:1,name:'Proj1',student:1 },
 { id:2,name:'Proj2',student:1},
 { id:3,name:'Proj3',student:2}
]; 

App.IndexRoute = Ember.Route.extend({
 model: function() {   
   return this.store.find('student');
 }
});

App.IndexController=Ember.ArrayController.extend({
  actions:{
    saveRecord:function(){
    var stu1=this.store.find('student',1);
    console.log(stu1.get('name'));  // RETURNS Undefined
  }
 }
});

在IndexController中,按ID查找记录不起作用。它的返回未定义。我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

您必须在find语句中使用promise:

this.store.find('student', 1).then(function(stu1) {
   // success
   console.log(stu1.get('name'));
}, function(error) {
   // error handling
   console.log(error);
});