简体   繁体   English

在ember.js中编辑模型

[英]Editing models in ember.js

I am trying to wrap my head around ember.js. 我试图把我的头缠在ember.js上。 I am writing a little web app that displays the prices for certain coffee beans. 我正在写一个小的Web应用程序,显示某些咖啡豆的价格。 Users can add new coffee beans, but now I want to give users the ability to edit existing coffee beans. 用户可以添加新的咖啡豆,但是现在我想让用户能够编辑现有的咖啡豆。 If a user double clicks on a bean's entry, then he or she should be able enter either a new name or price: 如果用户双击bean的条目,则他或她应该能够输入新名称或价格:

<script type="text/x-handlebars" data-template-name="coffee">
    <table>
      <tr>
        <th>Beans</th>
        <th>Prices</th>
      </tr>
      {{#each}}
      <tr>
        {{#if isEditing}}
          <td>{{input type="text"}}</td>
          <td>{{input type="text"}}</td>
          <td><button class="delete">Delete</button></td>
        {{else}}
          <td {{action "editCoffee" on="doubleClick"}}>{{bean}}</td>
          <td {{action "editCoffee" on="doubleClick"}}>{{price}}</td>
          <td><button class="delete">Delete</button></td>
        {{/if}}
      </tr>
      {{/each}}
    </table>

    {{input type="text" placeholder="Beans" value=newBean}}
    {{input type="text" placeholder="Price" value=newPrice}}
    <button type="button" {{action 'createCoffee'}}>Submit</button>

  </script>

Here is the code for the controller: 这是控制器的代码:

// Controllers
App.CoffeeController = Ember.ArrayController.extend({
  actions: {
    createCoffee: function() {
      // Get the bean name
      var bean = this.get('newBean');
      if (!bean.trim()) { return; }

      // Get the price
      var price = this.get('newPrice');
      if (!price.trim()) { return; }

      // Create the new coffee model
      var coffee = this.store.createRecord('coffee', {
        bean: bean,
        price: price
      });

      // Clear the text fields
      this.set('newBean', '');
      this.set('newPrice', '');

      // Save the new model
      coffee.save();
    },

    isEditing: false,

    editCoffee: function () {
      console.log('Hello World');
      this.set('isEditing', true);
    }

  }
});

Here is the link to the JS Fiddle: http://jsfiddle.net/cspears2002/y8MT3/ 这是JS小提琴的链接: http : //jsfiddle.net/cspears2002/y8MT3/

Double clicking a name or a price does get me into the editCoffee function, but I am not able to edit the coffee beans for some reasons. 双击名称或价格确实可以使我进入editCoffee函数,但是由于某些原因,我无法编辑咖啡豆。 Any ideas? 有任何想法吗?

There are a couple of problems. 有几个问题。 isEditing should live outside of the actions hash, and isEditing doesn't really exist on the ArrayController since the property is relevant to a single item, not the entire array. isEditing的靠外的actions乱码, isEditing并没有真正的存在ArrayController因为属性是相关的单个项目,而不是整个数组。 That being said an item controller would be an appropriate thing to use here. 话虽这么说,项目控制器将是在此处使用的适当方法。 In ember you can tell the array controller that there is an item controller that it should use when iterating over a list of items. 在ember中,您可以告诉数组控制器在迭代项目列表时应使用一个项目控制器。 One last note, tables cause a ton of problems in ember since it removes and insert dom into the page and depending on the browser this can cause a slew of problems with tables. 最后要注意的是,表会在余烬中引起大量问题,因为它会删除dom并将其插入页面中,并且取决于浏览器,这可能会导致许多表问题。 So I ripped out all of the table stuff for the sake of showing you how to fix it. 因此,为了展示如何修复它,我删除了所有表内容。

App.CoffeeItemController = Em.ObjectController.extend({    
  isEditing: false,

  actions: {
    editCoffee: function () {
      this.toggleProperty('isEditing');
    }
  }
});

App.CoffeeController = Ember.ArrayController.extend({
  itemController: 'coffeeItem'
  ....

http://jsfiddle.net/y8MT3/11/ http://jsfiddle.net/y8MT3/11/

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

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