87389

Ember.js Controller Does not work

I can't access to my array controller variables. I have this simple application for example:

App.js

App = Ember.Application.create(); App.ApplicationAdapter = DS.FixtureAdapter.extend(); App.Router.map(function() { this.route('hello'); }); App.IndexRoute = Ember.Route.extend({ redirect: function() { this.transitionTo('hello'); } }); App.HelloController = Ember.ArrayController.extend({ name: 'tom' }); App.HelloRoute = Ember.Route.extend({ model: function() { return this.store.find('hello'); } }); App.Hello = DS.Model.extend({ title: DS.attr('string') }); App.Hello.FIXTURES = [{ id: 1, title: 'hello' },{ id: 2, title: 'hello' }];

and my views are:

<script type="text/x-handlebars" data-template-name="application"> <h2>Welcome to Ember.js</h2> {{outlet}} </script> <script type="text/x-handlebars" data-template-name="hello"> {{#each}} {{title}} {{name}} {{/each}} </script>

when i save this code the page just render "hello" "hello" and i expect "hello tom" "hello tom". What i'm doing wrong?

Help is appreciated!

Answer1:

The problem is you're changing the context in your each statement to the model, so you no longer have the name in context.

http://emberjs.jsbin.com/UzUxIwa/1/edit

<script type="text/x-handlebars" data-template-name="hello"> {{#each item in controller}} {{item.title}} {{controller.name}} {{/each}} </script>

Honestly even better than this would be to use an itemController and put the property on that controller

http://emberjs.jsbin.com/UzUxIwa/2/edit

App.HelloController = Ember.ArrayController.extend({ itemController:'singleHello' }); App.SingleHelloController = Ember.ObjectController.extend({ name: 'tom' }); {{#each item in controller}} {{item.title}} {{item.name}}</br> {{/each}}

Recommend

  • Using set() on an array
  • Ember Link To Next Object In Model
  • Calling a method of view in emberjs
  • React-router Uncaught TypeError: type.toUpperCase is not a function
  • Spring 404 error
  • EmberJs: how to use connectOutlet
  • Simple NSTableView bindings example
  • Navigation within section of a page using react router
  • How to run java programs in grails?
  • How to access NSDocument object from another object held in NSMutableArray?
  • The handler mapping from the mvc:resource override other mappings which defined with annotation
  • No mapping found using Spring3 MVC + Maven2 on GAE
  • How to create mentions for names like “@myname” using javascript?
  • routing nested url in react-router
  • Animate Floating Action Button on ViewPager scroll
  • how to use the same method of jquery scrollTop with css3
  • Generate c# object code and assign values to its properties from an xml document
  • Strange behaviour of angular bootstrap collapse
  • Tweenmax callback being executed before the animation end
  • What are the best practices for migrating an Oracle 10g database to Microsoft SQL 2008 R2? Applicati
  • Text gets Blured during transition
  • xquery to return the element name
  • Change background color multiple times while scrolling down the page
  • css background transition in opera goes through black
  • How can I get rid of dynamic SQL
  • JQuery Mobile: data-direction=“reverse” not emulating initial transition
  • How can I obtain stationary distribution of a Markov Chain given a transition probability matrix
  • Animating an SVG Group
  • Unity 5.1 Animator Controller not transitioning
  • Spring Web Flow exception handling
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Laravel 4 routing not working due to .htaccess file?
  • Iron Router: How do I send data to the layout?
  • Jquery Mobile pageLoading() Method how does it work?
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?