1

I know i can find examples of this but i cant put it into my code without errors... just dont know why.

I want start with set class="menu-active" to first in menu.

<li><a {{action gotoAbout}} >About</a></li>

And later when somebody click other position of menu move class="menu-active" to this position.

http://jsfiddle.net/kwladyka/LGArM/3/

And the bonus question: Do you have any remarks to make my code better?

HTML
<script type="text/x-handlebars" data-template-name="application">
{{view App.NavbarView}}
{{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="navbar">
    <div id="menu" class="grid_12">
        <ul>
            <li><a {{bindAttr class="isAbout:active"}} {{action gotoAbout}} >About</a></li>
            <li><a {{bindAttr class="isProjects:active"}} {{action gotoProjects}} >Projekty</a></li>
            <li><a {{action gotoTechnology}} >Technologie</a></li>
            <li><a {{action gotoContact}} >Kontakt</a></li>
        </ul>
    </div>
</script>

EMBERJS

$(function() {

console.log("### emberjs start ###");

App = Em.Application.create({
    rootElement: '#emberjs-container'
});

App.ApplicationController = Em.Controller.extend();
App.ApplicationView = Em.View.extend({
    templateName: 'application'
});

App.NavbarController = Em.Controller.extend({

});
App.NavbarView = Em.View.extend({
    templateName: 'navbar',
});


App.AboutView = Em.View.extend({
    templateName: 'about'
});

App.ProjectsView = Em.View.extend({
    templateName: 'projects'
});

App.TechnologyView = Em.View.extend({
    templateName: 'technology'
});

App.ContactView = Em.View.extend({
    templateName: 'contact'
});


App.Router = Em.Router.extend({
    enableLogging: true,
    location: 'hash',

    root: Em.Route.extend({
        // EVENTS
        gotoAbout: Ember.Route.transitionTo('about'),
        gotoProjects: Ember.Route.transitionTo('projects'),
        gotoTechnology: Ember.Route.transitionTo('technology'),
        gotoContact: Ember.Route.transitionTo('contact'),

        // STATES
        about: Em.Route.extend({
            route: '/',
            connectOutlets: function (router, context) {
                router.get('applicationController').connectOutlet('about');
            }
        }),

        projects: Em.Route.extend({
            route: '/projects',
            connectOutlets: function (router, context) {
                router.get('applicationController').connectOutlet('projects');
            }
        }),

        technology: Em.Route.extend({
            route: '/technology',
            connectOutlets: function (router, context) {
                router.get('applicationController').connectOutlet('technology');
            }
        }),

        contact: Em.Route.extend({
            route: '/contact',
            connectOutlets: function (router, context) {
                router.get('applicationController').connectOutlet('contact');
            }
        })
    })
});

App.initialize();

});
Mike Grassotti
  • 19,040
  • 3
  • 59
  • 57
kabra
  • 1,306
  • 1
  • 17
  • 24
  • See pangratz's answer at http://stackoverflow.com/questions/11628489/emberjs-how-to-mark-active-menu-item-using-router-infrastructure – zaplitny Aug 01 '12 at 14:21
  • What exatly do: selectedBinding: 'controller.selected' I dont have any idea how this line was created and whats going on. – kabra Aug 01 '12 at 19:48
  • 1
    Ember.View has property called 'controller' and by naming convention it's bind to controller starting the same, e.g MyView and MyController. Read about bindings here at http://emberjs.com/documentation/#toc_bindings . And don't forget to use latest ember.js as 0.9.8 release is old. You can find it at http://cloud.github.com/downloads/pangratz/ember.js/ember-latest.js – zaplitny Aug 01 '12 at 20:45
  • Where can i read about Controller? In http://docs.emberjs.com i can read about ArrayController etc. but where can i read about Controller? I dont fill why using `selectedBinding: 'controller.selected'`. Why not directly View and set by `router.set('navigationView.selected', 'profiles');`? How to know when use View and when Controller? – kabra Aug 01 '12 at 20:57
  • The best documentations now is framework sources. But I believe it will change soon and there will be lot of great articles. As for selected property... Good question :-). I'm newbie too and can't tell what is good and what is not. There's good slides from Tom Dale at https://speakerdeck.com/u/tomdale/p/emberjs-more-than-meets-the-eye . May be they can help – zaplitny Aug 01 '12 at 21:25
  • 2
    In your example, you cannot call router.set('navigationView.selected', 'profiles'), simply because it's not accessible via the router. The way for the router is to stimulate the controller (here selected property), and the view have a binding on this property. Perhaps it seems to be complicated, but this is the MVC implementation in ember. The user gestures are on the views, wich redirects events on the router, witch modify the state of the application via the controller. These changes are reflected to the view via bindings/observers defined in the view on it's controller.(I hope to be clear) – sly7_7 Aug 01 '12 at 21:41
  • huh i dont understand that. Any lecture or example to understand? – kabra Aug 03 '12 at 20:47
  • classNameBindings: "isActive:menu-active".w() - what w() do? – kabra Aug 04 '12 at 08:46

0 Answers0