【问题标题】:ember js - controller action not invokedember js - 未调用控制器操作
【发布时间】:2015-11-12 04:01:41
【问题描述】:

我有以下控制器代码:

var App = Ember.Application.create();


App.DocumentController = Ember.Controller.extend({  
  actions:{
    test:function(){
      console.log('test');
    }
  }
});

这个表格:

div class="form-horizontal form-group form-group-lg row">
    <div class="col-xs-10 col-xs-offset-1 col-sm-6 col-sm-offset-1 col-md-5 col-md-offset-2">
      {{input type="text" value=name class="form-control" placeholder="Name of document." autofocus="autofocus"}}
    </div>
    <div class="col-xs-10 col-xs-offset-1 col-sm-offset-0 col-sm-4 col-md-3">
         <button class="btn btn-primary btn-lg btn-block" {{action 'test'}}>Create Document</button>
    </div>

</div>

{{#if responseMessage}}
  <div class="alert alert-success">{{responseMessage}}</div>
{{/if}}

点击按钮时,调试器输出:

没有处理“firstAction”动作。如果您确实处理了该操作,则此错误可能是由于从控制器中的操作处理程序返回 true 导致该操作冒泡

不知道是什么问题——路由代码:

Router.map(function() {  
  this.resource( 'index', { path: '/' } );
  this.route('decisions');
  this.route('teams');
  this.route('about');
  this.resource("documents",{ path: '/documents' }, function() {            
        this.route('/show', {path: '/show/:id'});
        this.route('/edit', {path: ':id/edit'});
  });
});

型号代码:

import DS from 'ember-data';

export default DS.Model.extend({  
  name: DS.attr('String'),
  url: DS.attr('String'),
  text: DS.attr('String')  
});

我知道 ember 有一个严格的命名约定,但我使用他们教程中的命名约定从 ember-cli 生成了资源(文档)。如何让模板 hbs 代码与控制器对话?

【问题讨论】:

    标签: ember.js ember-data ember-cli


    【解决方案1】:

    问题出在我的文件夹结构上。因为文档模型会有很多不同的模板,所以我决定在模板文件夹中添加一个文档文件夹。我应该对控制器文件夹做同样的事情(即添加一个文档文件夹)。此外,控制器的文件名需要与模板的文件名相同(在这种情况下 index.hbs 为模板,index.js 为控制器文件)。

    【讨论】:

      【解决方案2】:

      您的控制器中的操作名称为 test,但您正在调用名称为 firstAction 的方法,该名称未定义。

      试试下面的:

       <button class="btn btn-primary btn-lg btn-block" {{action 'test'}}>Create Document</button>
      

      【讨论】:

      • 抱歉,我在写问题时打错了字,编辑了问题中的操作名称。仍然遇到同样的问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多