Ember组件sendAction()无法正常工作

Wil*_*rts 16 javascript ember.js ember-data

在过去的几个小时里,我一直在努力解决这个问题,我正在制作一个用于创建发票的余烬应用程序.我正在使用ember组件(textfield)使用键盘修改字段,但由于操作不会发送回相关的控制器,我无法将记录保存在focusOut或insertNewLine上,并且没有任何事情发生.我在用 :

Ember      : 1.1.2 
Ember Data : 1.0.0-beta.3 
Handlebars : 1.0.0 
jQuery     : 1.9.1
Run Code Online (Sandbox Code Playgroud)

这应该是这样的:https://dl.dropboxusercontent.com/u/7311507/embercomponent.png

问题似乎在控制器或组件内,似乎我缺少一些东西.

在组件上调用console.log函数,sendAction调用永远不会...

谢谢您的帮助.

ItemsRoute

App.ItemsRoute = Ember.Route.extend({
    renderTemplate: function() {
          // Render default outlet   
          this.render();
          // render extra outlets
          this.render("client", { outlet: "client", into: "application"});
      },
      model: function() {
        return this.store.find('item');
      }
    });
Run Code Online (Sandbox Code Playgroud)

上述ItemsController

App.ItemsController = Em.ArrayController.extend({
    actions: {
      createItem: function () { // NEVER GETS CALLED FROM COMPONENT
        var title = "Nouvel élément"

        // Create the new Todo model
        var item = this.store.createRecord('item', {
          desc: title,
          qty: 1,
          price: 0
        });

        // Save the new model
        item.save();
      }
    },
    totalCount: function(){
        var total = 0;
        this.get('model').forEach(function(item){
            total += item.get('totalprice');
        });
        return total;
    }.property('@each.qty', '@each.price')
});
Run Code Online (Sandbox Code Playgroud)

ItemController

App.ItemController = Em.ObjectController.extend({
    didInsertElement: function(){
        this.$().focus();
    },
    actions: {
        testAction: function(){ // NEVER GETS CALLED FROM COMPONENT
            console.log("controller recieved call for testAction");
        },
        saveItem: function(value) {
            this.get('model').save();

        },
        removeItem: function() {
            var item = this.get('model');
            item.deleteRecord();
            item.save();
          },
    },
    isHovering: false
});
Run Code Online (Sandbox Code Playgroud)

项目模板

<script type="text/x-handlebars" data-template-name="items">
      <!-- ...  -->

      <tbody>
      {{#each itemController="item"}}
        {{view App.ItemView }}
      {{/each}}
      </tbody>

      <!-- ... -->
  </script>
Run Code Online (Sandbox Code Playgroud)

ItemView模板

<script type="text/x-handlebars" data-template-name="item">
    <td class="desc">{{edit-item value=desc}}</td>
    <td class="qty">{{edit-item-number value=qty }}</td>
    <td class="">{{edit-item-number step="25" value=price}}</td>
    <td class="totalprice">
      {{ totalprice }}
      <div class="delete-item" {{bindAttr class="isHovering"}} {{action "removeItem" on="click"}}>
        <i class="icon-trash"></i>
      </div>
    </td>
  </script>
Run Code Online (Sandbox Code Playgroud)

视图/组件

App.ItemView = Em.View.extend({
    templateName: "item",
    tagName: "tr",

    mouseEnter: function(event) {
        this.get('controller').set('isHovering', true);
    },
    mouseLeave: function(event) {
        this.get('controller').set('isHovering', false);
    }
});

App.EditItem = Em.TextField.extend({
    becomeFocused: function() {
        this.$().focus();
    }.on('didInsertElement'),

    insertNewline: function(){
        console.log('Tried to insert a new line'); // WORKS
        this.triggerAction('createItem'); // DOESN'T WORK
    },

    focusOut: function(){
        console.log('Focused the Field Out') // WORKS
        this.triggerAction('testAction', this); // DOESN'T WORK
    }

});

App.EditItemNumber = App.EditItem.extend({
    becomeFocused: null,
    attributeBindings: ["min", "max", "step"],
    type: "number",
    min: "0"
});

Ember.Handlebars.helper('edit-item', App.EditItem);
Ember.Handlebars.helper('edit-item-number', App.EditItemNumber);
Run Code Online (Sandbox Code Playgroud)

Kin*_*n2k 34

您应该定义在模板中定义组件时将发送操作的位置.

{{edit-item value=desc createItem='someactionoutside'}}
Run Code Online (Sandbox Code Playgroud)

这是因为动作在不同的地方有不同的名称(因为这是一个组件,它可能在不同的位置有不同的含义).它还避免了冲突动作/触发动作.想一想有两个组件实例的想法,每个实例都应该在控制器中触发不同的操作

{{edit-item value=desc createItem='createUser'}}
{{edit-item value=desc createItem='createShoppingCart'}}
Run Code Online (Sandbox Code Playgroud)

在你的情况下,你可以写

{{edit-item value=desc createItem='createItem'}}
Run Code Online (Sandbox Code Playgroud)

在您的组件内部,您可以打电话

this.sendAction('createItem', param1, param2, ....);
Run Code Online (Sandbox Code Playgroud)

如果您不关心它像组件一样自包含,您可能只想使用视图而不是组件.你可以把它注册为帮手,它看起来很漂亮.

Em.Handlebars.helper('edit-item', Em.View.extend({
  templateName: 'some_template',

  actions: function(){
   // etc etc
  } 

})); 

{{edit-item}}
Run Code Online (Sandbox Code Playgroud)