2014-01-15 31 views
0

我的代碼:查找記錄在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 
    } 
} 
}); 

在索引控制器通過ID查找記錄不工作。其回報未定義。我的代碼有什麼問題?

回答

0

你必須使用一個承諾與您發現聲明:

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