【问题标题】:EmberJS :reload/refresh view after creating new record while using jquery ajax instead of ember-dataEmberJS:在使用 jquery ajax 而不是 ember-data 创建新记录后重新加载/刷新视图
【发布时间】:2015-11-30 15:43:16
【问题描述】:

我正在使用 jquery 创建一个示例 todo 应用程序来与 rest api 而不是 ember-data 对话。代码如下。我可以使用此代码列出和添加待办事项。但是一旦我创建了一条新记录,我必须刷新浏览器才能看到新记录。如何让控制器重新加载模型?

这是路线

App.TodosRoute = Ember.Route.extend({
 model: function() {
  return Ember.$.getJSON("http://localhost:3000/todos");
 }
}); 

这是控制器,我可以在其中添加新的待办事项

App.TodosController = Ember.ArrayController.extend({
actions: {
    addTodo: function(){
        var newTodo=this.get('newTodo');
        this.set('newTodo', '');
        Ember.$.post("http://localhost:3000/todos",{todo: newTodo}));
    }
}
});

这是模板

<script type="text/x-handlebars" data-template-name="todos">
<legend>Todos</legend>
{{input type="text" id="new-todo" value=newTodo placeholder="Todo" action="addTodo" }}

<ul>
{{#each}}
<li><a href='#'>{{todo}}</a></li>
{{/each}}
</ul>
</script>

【问题讨论】:

    标签: ember.js ember.js-view


    【解决方案1】:

    在当前路由模型中添加新的待办事项。

    App.TodosController = Ember.ArrayController.extend({
       actions: {
          addTodo: function(){
          var newTodo=this.get('newTodo');
          this.set('newTodo', '');
          self = this
          Ember.$.post("http://localhost:3000/todos",{todo:newTodo}).then(function(response)               {
             self.addObject(response);
         });
       }
     }
    });
    

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 2019-08-23
      • 2017-02-07
      • 2014-10-17
      • 2021-11-19
      • 2012-07-22
      相关资源
      最近更新 更多