Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ember.js <button {{action}}></button> doesn't work properly

Tags:

ember.js

I have http://jsfiddle.net/pF2cF/6/ code with 2 problems: 1. click "MyButton" doesn't go to clickButton function in App.indexController 2. enter in the text field will trigger MyButton click first (if #1 get solved)

Can anyone help to solve them? I do have a workaround for by using , but I'm not sure what's wrong with using.

Thanks!

The code snippets are as following, using ember.js from its master branch on 01/14/2013:

    <script type="text/x-handlebars" data-template-name="myTemplate">
        <button {{action clickButton target="App.indexController"}} >MyButton1</button>
        {{view App.MyView placeholder="Input something 1 and enter"}}
    </script>

App = Em.Application.create({
  ready: function () {
  }
});

App.Router.map(function () {
  this.route("index", { path: "/" });  //master 01142013 syntax
});
App.IndexRoute = Ember.Route.extend({
  renderTemplate: function () {
    this.render('myTemplate', {controller: 'indexController'});
  }
});

App.indexController = Ember.Controller.extend({
  clickButton: function () {
    alert("clickButton");
  }
});

App.MyView = Em.TextField.extend({
  insertNewline: function (evt) {
    alert("enter pressed");
  }
});
like image 342
xinqiu Avatar asked Jan 18 '13 07:01

xinqiu


People also ask

How do you pass the action to the component in Ember?

Passing the Action to the Componentimport Component from '@ember/component'; export default Component. extend({ actions: { launchConfirmDialog() { this. set('confirmShown', true); }, submitConfirm() { //call the onConfirm property to invoke the passed in action this.

What is closure action ember?

What are Ember closure actions? Ember closure actions are based on JavaScript closures which are basically functions that remember environment in which they were created. So closure actions are just functions that remember context in which they were defined.

How do you call a child component method from parent in Ember?

The parent can then use this reference to the child component to call its function whenever required. I decided to place the sendAction call inside didInsertElement hook of the child element to register itself with the parent component as soon as it is added to the parent DOM. Following is the gist to the solution.

Is Ember js a frontend?

Ember. js is a frontend web framework which claims on its website, to be the one tool you will need to build an ambitious web application.


1 Answers

You made a number of small errors. I have put a working version on JSBin.

Stylistic issues that did not cause any failures:

  • You don't need to declare any index routes.
  • You don't need an empty ready method on Application. In general, put startup logic in ApplicationRoute#setupController, where you will also have access to controllers.
  • You should name your template the same name as the route unless you are trying to share a single template across multiple routes. In this case, you should have just used index rather than myTemplate.

Issues related to the failures:

  • Controller names should be capitalized: App.IndexController not App.indexController
  • You shouldn't specify a target if the target is the current controller.
  • Your render call specifies { controller: 'indexController' }. It should be { controller: 'index' }.
like image 193
Yehuda Katz Avatar answered Sep 24 '22 14:09

Yehuda Katz