2

I have been trying to learn Ember, I have written a very simple routing example by following other examples here on SOF. I have three views:

Home Mars Jupiter

I would like to have three nav links that would call/resolve to mentioned views. I don't what am I doing wrong, I cannot seem to get any output at all. I would highly appreciate if if someone can point out the faults.

Here is the fiddle: http://jsfiddle.net/combustion007/PvCk8/

APP CODE:

$(function()
{
    App = Em.Application.create({
        name: "superman",
        init: function(){
            alert("APP INIT");
        }
    })

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

    //  NAVBAR
    App.NavbarController = Em.Controller.extend();
    App.NavbarView = Em.View.extende({
        templateName: 'navbar',
        classNames: ['']
    });

    //  HOME
    App.HomeController = Em.Controller.extend();
    App.HomeView = Em.View.extend({
        tempalteName: 'home',
        classNames: ['']
    });

    //  MARS
    App.MarsController = Em.Controller.extend();
    App.MarsView = Em.View.extend({
        templateName: 'mars',
        classNames: ['']
    });

    //  JUPITER
    App.JupiterContoller = Em.Controller.extend();
    App.JupiterView = Em.View.extend({
        templateName: 'jupiter',
        classNames: ['']
    });

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

        //  EVENTS
        root: Em.Route.extend({
            gotoHome: Ember.Route.transitionTo('home'),
            gotoMars: Ember.Route.transitionTo('mars'),
            gotoJupiter: Ember.Route.transitionTo('jupiter'),

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

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

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

HTML:

<script type="text/x-handlebars" data-template-name="application">
    {{view App.NavbarView controllerBinding="controller.controllers.navbarController"}}
    <br /><hr />
    <div class="content">
        {{outlet}}
    </div>
</script>

<script type="text/x-handlebars" data-template-name="navbar">
    <ul>
        <li><a href="#" {{action gotoHome}}>Home</a></li>
        <li><a href="#" {{action gotoMars}}>Mars</a></li>
        <li><a href="#" {{action gotoJupiter}}>Jupiter</a></li>
    </ul>
</script>


<script type="text/x-handlebars" data-template-name="home">
    <h2>Home</h2>
    <hr />
    <br /><br />          
</script>


<script type="text/x-handlebars" data-template-name="mars">
    <h2>Mars</h2>
    <hr />
    <br /><br />          
</script>

<script type="text/x-handlebars" data-template-name="jupiter">
    <h2>Jupiter</h2>
    <hr />
    <br /><br />          
</script>
Combustion007
  • 474
  • 1
  • 13
  • 30
  • +1 for providing the jsFiddle, please make sure to go over your fiddles once to check for typos. Looking at console output will also help you a lot in identifying the issues. Hope my response will be helpful. – Aras Dec 16 '12 at 09:39

2 Answers2

2

Changes to your jsfiddle http://jsfiddle.net/cteegarden/PvCk8/7/:

  • Added handlebars js library
  • fixed 2 typos: extende changed to extend and tempaltName to templateName
  • updated the mars and jupiter routes to be accessible at route's other than '/'
  • removed the init method on App

The {{outlet}} in the application handlebars template is filled with the home view by default when the application is in the '/' state, mars view when the application is in the '/mars', and the jupiter view when the application is in the '/jupiter'. The 3 links you have in the navbar template programmatically change the state to each of those, causing the {{outlet}} to be replaced with the correct view.

Edit

Two resources with examples of similar routing

Community
  • 1
  • 1
CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
0

I went through your jsFiddle and fixed a number of things to make it work: http://jsfiddle.net/arasbm/fLBty/3/

It still doesn’t do what you want, because there are some things that you need to decided on. The main issue with that code was that / was defined three times in the router. You only need to define it once and then connect all the outlets inside that route:

root: Em.Route.extend({

    whatever: Em.Route.extend({
        route: '/',
        connectOutlets: function(router, context){
            router.get('applicationController').connectOutlet('home_outlet', 'home');
            router.get('applicationController').connectOutlet('mars_outlet', 'mars');
            router.get('applicationController').connectOutlet('jupiter_outlet', 'jupiter');
        }
    })            
})

Because in this case multiple outlets are used inside the same route you need to name the outlet:

<script type="text/x-handlebars" data-template-name="application">
    {{view App.NavbarView}}
    <br /><hr />
    {{outlet home_outlet}}
    {{outlet mars_outlet}}
    {{outlet jupiter_outlet}}
</script>

I have only been using ember for a few months, so I don’t have a very good perspective of it yet. I hope this will help you a bit to move forward.

Edit: Updated the answer based on comment about the new router API not being released yet.

If you are just starting out with ember take a look at the router API v2 guide by tomdale which is the most up to date information about how router will be defined in Ember 1.0. But as was pointed out in the comments, it is not in the master yet.

Aras
  • 5,878
  • 9
  • 49
  • 76
  • thank you so much for your help. No, I don't insist upon following the old ways at all. That is what I thought was the right way, I am following the online documentation at Ember.com. I will go through you posted link and see if I can make it work by implementing the revised way. Would it be possible 4 u to do a very simple fiddle showing what I am trying do with my code. I would highly appreciate it. Thx – Combustion007 Dec 16 '12 at 17:51
  • 1
    @Combustion007 the v2 router API has not yet been merged into master. No one I know is using it yet. – Luke Melia Dec 16 '12 at 22:14
  • thanks for pointing that out @LukeMelia, I updated my answer. Next time I will make sure not suggest something I haven’t tried myself. For some reason I was under the impression that the new router API was available in ember-latest, but as you pointed out -- it is not. – Aras Dec 17 '12 at 02:04
  • @Combustion007 sorry about giving you wrong advice, as I said I am pretty new too. Please look at my updated answer. – Aras Dec 17 '12 at 02:08
  • First of all, thank you @Luke and @ Aras for all your help.I have been spending serious time with Ember.com/documentations and trying to implement this framework for all my projs.I m rather confused now knowing there are changes coming in near future.Should I continue to code what currently suggested practises are on Ember.com? All I want is to get going with this framework, and for the past year or so, it was either poorly done online documentation or just enuff resources.And I see tons of online example such Andy.Mathews or TomDale. But it is still wild wild west. – Combustion007 Dec 17 '12 at 05:15
  • 1
    Guys would it be possible to help me put a small logic together which demos routing and and nested routing such as "SubMenus", the way you you would program it. Something very simple so folks like me have a good referrence to go by. Honestly, hours and hours googling, finding fiddles, then checking what Ember Ver they are running just takes a long time which I don't mind, but nothing solid materializes. I have about 37 fiddles bookmarked which I found via Google. I have this fiddle which I have uploaded, I need help to under what am I doing wrong, plz. Appreciate all your help – Combustion007 Dec 17 '12 at 05:34
  • @Combustion007 I hear ya! All I am doing to cope with the changes is to try to stay on latest ember build and follow what is happening, but it is not always easy. Maybe ask for some guidance on #emberjs irc channel about this. – Aras Dec 17 '12 at 05:45
  • Last night, I watched Andy Mathews' "Getting Started with EmberJS" at "http://vimeo.com/44315616". It was waste of time, poor quality and off target. I speak for myself, I could careless about what tools Paul Irish is currently using whether it be Grunt or Z. I watched the presentation and walked away with very little or none. One can be a superb coder but not necessarily great at teaching which seem to be the going trend whether you watch these developers on Vimeo or YouTube. – Combustion007 Dec 17 '12 at 05:50
  • Thank you so much Aras for all your help. I think I am going to explore other JavaScript frameworks. – Combustion007 Dec 17 '12 at 05:53
  • @Combustion007 Here is an example of what you are asking for: http://stackoverflow.com/questions/11318572/right-way-to-do-navigation-with-ember and this tutorial also has an example: http://codebrief.com/2012/07/anatomy-of-an-ember-dot-js-app-part-i-redux-routing-and-outlets/ – CraigTeegarden Dec 17 '12 at 21:12
  • Thank you so much c4p, I absolutely appreciate your help. – Combustion007 Dec 18 '12 at 00:13