I have been trying to understand Emberjs for some time now, I've done the "To Do" application on the Getting Started link, I've read all the documentation and I've coded the blog from the "Ember in Action" book.
Now I am trying to do something on my own and it turns out that I am having all sort of problems so I'll start with the first one on this post.
I have come up with the following templates:
<body>
<script type="text/x-handlebars" data-template-name="user-navigation">
<ul id="userNavigation">
{{#if isAuthenticated}}
<li>Welcome {{userName}}</li>
<li>{{#link-to "awesome.index"}}logout{{/link-to}}</li>
<li><a href="#">Settings</a></li>
{{else}}
<li>{{#link-to "awesome.login"}}login{{/link-to}}</li>
<li><a href="#">sign up</a></li>
{{/if}}
</ul>
</script>
<script type="text/x-handlebars" data-template-name="awesome/index">
{{#if isAuthenticated}}
<section id="userWidgets">
<div>
<button id="createNewWidget">Create New Widget</button>
</div>
<div>
<ul>
{{#each widget}}
<li><a href="#">{{widgetName}}</a></li>
{{/each}}
</ul>
</div>
</section>
<section id="selectedWidget">
</section>
{{else}}
<div id="welcome">Welcome to my awesome Site. You should sing up!</div>
{{/if}}
</script>
<script type="text/x-handlebars" data-template-name="awesome/login">
<ul id="mainLoginOptions">
<li><button class="link-button" {{action 'facebookLogin'}}>Login with Facebook</button></li>
</ul>
</script>
<script type="text/x-handlebars" id="application">
<header id="siteHeader">
<div id="logo" class="index-logo">My awesome SPA</div>
<div id="userData" class="index-userdata">
{{render "user-navigation" user}}
</div>
</header>
<section id="mainContent">
{{outlet}}
</section>
<footer id="footer" class="index-footer"><div class="index-footer-content">Copyright 2014</div></footer>
</script>
</body>
Basically it is a header at the top with "log in/signup" or "log out/settings" links depending if the user is logged in or not. And in the main content section a "Welcome" message or the logged in user's content.
I am trying to figure out how to handle the log in workflow so the header template renders correctly and the main content renders with the user's data. I was looking at this link to get ideas but I still have a lot of questions about how to pull it off.
My JavaScript code looks like this:
window.Awesome = Ember.Application.create({
LOG_TRANSITIONS: true
});
Awesome.Router.map(function() {
this.resource("awesome", {path: "/"}, function(){
this.route('login');
});
});
Awesome.AwesomeLoginRoute = Ember.Route.extend({
actions:{
facebookLogin: function(){
var router = this;
Awesome.User = Ember.Object.create({
id: "1",
isAuthenticated: "true",
userName: "Serge"
});
router.transitionTo('awesome.index');
}
}
});
Not much but, based on that link, I am setting a "User" object that is defined directly in the application but I do not know how to send it to the templates so that they use it to render themselves correctly.
Of course the idea is that at some point I will be using a service to get the tokens from different social networks but the first step is to understand how Ember works and for that I am trying to stub the object.
Any pointers are very much appreciated.