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

时间:2013-11-20 22:26:13

标签: 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

这应该是这样的: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');
      }
    });

上述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')
});

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
});

项目模板

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

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

      <!-- ... -->
  </script>

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>

观看/组件

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);

2 个答案:

答案 0 :(得分:34)

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

{{edit-item value=desc createItem='someactionoutside'}}

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

{{edit-item value=desc createItem='createUser'}}
{{edit-item value=desc createItem='createShoppingCart'}}

在你的情况下,你可以写

{{edit-item value=desc createItem='createItem'}}

在您的组件中,您可以调用

this.sendAction('createItem', param1, param2, ....);

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

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

  actions: function(){
   // etc etc
  } 

})); 

{{edit-item}}

答案 1 :(得分:2)

作为@Kingpin2k的优秀答案的补充,您还可以在组件中定义您的操作名称,如果它始终相同并且您希望简化包含组件的语法。即

import Ember from 'ember';
export default Ember.Component.extend(SchoolPlayerProspectMixin, {

    //Here we define an attribute for a string that will always be the same
    transitionToRoute: "transitionToRoute",

    somethingChanged: function(){
        console.log( "OMG something changed, lets look at a post about it!" );

        //Here we are passing our constant-attribute to the sendAction.
        self.sendAction('transitionToRoute', "post.show", post );

    }.observes('changableThing'),
});

在此示例中,组件使用父控制器transitionToRoute方法来更改路由,即使该组件可能不是按钮/链接。例如,导航更改包含多个选择输入的组件,或者只更改组件内的路径。

相关问题