Ember 视图不会随着模型发生的变化立即更新
Ember View does not updating as of the change that happen to the model immediately
当我发出 AJAX 更新某些值的请求时,数据或模型正在发生变化。但是更新后的模型并没有立即反映出来。它仅在单击刷新后才会反映。我只想修改页面中一个 div 的视图。我尝试了很多东西但没有成功。谁能建议我解决问题的方法?
git 回购:
https://github.com/SyamPhanindraChavva/trell-app-front-end
templates/note.hbs
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<p><a class="dropdown-item" {{action 'updateNote' "doing" note.id}}>DOING</a></p>
<p><a class="dropdown-item"{{action 'updateNote' "done" note.id}}>DONE</a></p>
<p><a class="dropdown-item"{{action 'deleteNote' note.id}}>DELETE</a></p>
</div>
controllers/notes.js
status2:Ember.inject.service('work-status'),
actions: {
updateNote(upstatus,id){
let status1 = Ember.get(this,'status2');
status1.makeputrequest(upstatus,id);
this.toggleProperty('parameter');
}
}
services/work-status.js
makeputrequest(status1,id1)
{
$.when($.ajax ({
// get(this,'layout.referrer.owner.router').transitionTo('notes');
type: 'PUT',
url:'http://localhost:3000/notes/'+id1,
data: {note:{status:status1}},
})).then(function(result){
console.log(result);
}).catch(function(err){
console.log("you have error")
});
},
我想做的是,每当我更改记录的状态时,UI 中的相应记录也必须转到 UI 中相应的 table 或容器.
因为您直接在 ember 中使用 ajax 调用,您将负责在完成后设置新属性。
在 services/work-status.js 中,您需要 return ajax 调用生成的承诺。
在 controllers/notes.js 中,将 .then 链接到 returned promise 并调用并设置新的 属性。
EG - controllers/notes.js
updateNote(upstatus,id){
let status1 = Ember.get(this,'status2');
status1.makeputrequest(upstatus,id).then((){
this.get('model').findBy('id',id).set('status',upstatus);
});
},
话虽如此,这不是正确的路线。您将 ember-data 与纯 ajax 调用混合在一起,这使得您的代码库逻辑变得比必要的更困难。
如果您修改操作以将整个笔记作为参数,更改状态并仅保存模型,则无需使用该服务。
// templates/note.hbs
<p><a class="dropdown-item" {{action 'updateNote' note "doing"}}>DOING</a></p>
// controllers/note.js
updateNote(note,upstatus) {
note.set('status',upstatus)
note.save();
}
Ajax 仅表示您不想更改记录中的内容。但是,如果您想更改记录的值,您应该使用 ember-data
当我发出 AJAX 更新某些值的请求时,数据或模型正在发生变化。但是更新后的模型并没有立即反映出来。它仅在单击刷新后才会反映。我只想修改页面中一个 div 的视图。我尝试了很多东西但没有成功。谁能建议我解决问题的方法? git 回购: https://github.com/SyamPhanindraChavva/trell-app-front-end
templates/note.hbs
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<p><a class="dropdown-item" {{action 'updateNote' "doing" note.id}}>DOING</a></p>
<p><a class="dropdown-item"{{action 'updateNote' "done" note.id}}>DONE</a></p>
<p><a class="dropdown-item"{{action 'deleteNote' note.id}}>DELETE</a></p>
</div>
controllers/notes.js
status2:Ember.inject.service('work-status'),
actions: {
updateNote(upstatus,id){
let status1 = Ember.get(this,'status2');
status1.makeputrequest(upstatus,id);
this.toggleProperty('parameter');
}
}
services/work-status.js
makeputrequest(status1,id1)
{
$.when($.ajax ({
// get(this,'layout.referrer.owner.router').transitionTo('notes');
type: 'PUT',
url:'http://localhost:3000/notes/'+id1,
data: {note:{status:status1}},
})).then(function(result){
console.log(result);
}).catch(function(err){
console.log("you have error")
});
},
我想做的是,每当我更改记录的状态时,UI 中的相应记录也必须转到 UI 中相应的 table 或容器.
因为您直接在 ember 中使用 ajax 调用,您将负责在完成后设置新属性。
在 services/work-status.js 中,您需要 return ajax 调用生成的承诺。
在 controllers/notes.js 中,将 .then 链接到 returned promise 并调用并设置新的 属性。
EG - controllers/notes.js
updateNote(upstatus,id){
let status1 = Ember.get(this,'status2');
status1.makeputrequest(upstatus,id).then((){
this.get('model').findBy('id',id).set('status',upstatus);
});
},
话虽如此,这不是正确的路线。您将 ember-data 与纯 ajax 调用混合在一起,这使得您的代码库逻辑变得比必要的更困难。
如果您修改操作以将整个笔记作为参数,更改状态并仅保存模型,则无需使用该服务。
// templates/note.hbs
<p><a class="dropdown-item" {{action 'updateNote' note "doing"}}>DOING</a></p>
// controllers/note.js
updateNote(note,upstatus) {
note.set('status',upstatus)
note.save();
}
Ajax 仅表示您不想更改记录中的内容。但是,如果您想更改记录的值,您应该使用 ember-data