简体   繁体   English

从控制器更新Ember模型

[英]Update Ember model from controller

currently i try to create like infinite scroll by putting some button in the template to load more data. 目前,我尝试通过在模板中放置一些按钮来加载更多数据来创建无限滚动。

bellow is the code 波纹管是代码

index.hbs index.hbs

{{#each model}}
  <p>{{name}}</p>
{{/each}}
<button {{action "addMore"}}>Add More</button>

controller/index.js 控制器/ index.js

import Ember from 'ember';

export default Ember.ArrayController.extend({
  actions:{
    addMore: function (){
      this.get("model").push({name:"some data"});
    }
  }
});

and route index.js 和路由index.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    var v=[];
    return v;
  }
});

The problem was, after user clicked the button, the controller pushed the data into the model, but the template did not show model with current data (data after the controller push new data into the model). 问题在于,用户单击按钮后,控制器将数据推送到模型中,但是模板没有显示具有当前数据的模型(控制器将新数据推送到模型中之后的数据)。

please help 请帮忙

thanks 谢谢

A few things that are wrong/deprecated in your code: 您的代码中有一些错误/不建议使用的东西:

export default Ember.ArrayController.extend becomes export default Ember.Controller.extend export default Ember.ArrayController.extend成为export default Ember.Controller.extend

  • {{#each model}} now takes the form of {{#each model as |myNameObject|}}} (the old way still works but it is recommended to use block scoping) and in the template you would use {{myNameObject.name}} {{#each model}}现在采用{{#each model as |myNameObject|}}} (仍然可以使用旧方法,但是建议使用块作用域),并且在模板中应使用{{myNameObject.name}}

  • You are using .push when you should be using pushObject 您应该使用pushObject时使用.push

JsBin demo. JsBin演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM