This is my first Ember.js application. All Ember's examples and tutorials I found are a single controller with a single model or a single model having children models. However, this application puts two models (companies and users) into a single controller (login) side by side. However it doesn't work very well.
Its jsbin.com code is at: jsbin.com/cirah
My design is: the login controller doesn't have a defined model, but two arrays of companies and users. Each of both has an _selid to indicate the current selection.
window.App = Ember.Application.create({});
App.ApplicationStore = DS.Store.extend({
adapter: 'App.ApplicationAdapter',
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://emberjs.azurewebsites.net',
headers: {
"Accept": "application/json",
}
});
App.Company = DS.Model.extend({
company: DS.attr('string')
});
App.User = DS.Model.extend({
userName: DS.attr('string'),
passwordHash: DS.attr('string'),
});
App.Router.map(function () {
this.resource("login", { path: '/login' });
});
App.IndexRoute = Ember.Route.extend({
redirect: function () {
this.transitionTo('login');
},
});
App.LoginRoute = Ember.Route.extend({
setupController: function (controller, model) {
var store = this.get('store');
controller.set('companies', store.find('company'));
controller.set('company_selid', 2);
controller.set('users', store.find('user'));
controller.set('user_selid', 3);
},
});
App.LoginController = Ember.ObjectController.extend({
companies: null,
company_selid: '2',
users: null,
user_selid: '3',
passwordHash: '',
actions: {
login: function () {
alert('login clicked');
},
},
});
The login template contains two Ember.Select(s) for the arrays.
<div class="input-group">
<label for="company" class="input-group-addon ">Company:</label>
{{view Ember.Select
class="form-control"
content=companies
optionValuePath="content.id"
optionLabelPath="content.company"
value="company_selid"
selectionBinding="company_selid"}}
</div>
<div class="input-group">
<label for="userName" class="input-group-addon">User Name:</label>
{{view Ember.Select
class="form-control"
content=users
optionValuePath="content.id"
optionLabelPath="content.userName"
value=user_selid
selection=user_selid
}}
</div>
The server side returns:
http://emberjs.azurewebsites.net/companies?format=json
{"companies":[
{"id":1,"company":"ABC Company"},
{"id":2,"company":"XYZ Company"}
]}
http://emberjs.azurewebsites.net/users?format=json
{"users":[
{"id":101,"userName":"Aaron","passwordHash":""},
{"id":102,"userName":"Brian","passwordHash":""},
{"id":103,"userName":"Corey","passwordHash":""},
{"id":104,"userName":"David","passwordHash":""}
]}
My questions are:
Q1: What's the Ember's best practice to use single controller to embed multiple models? I'd like to have eager loading of two arrys, should I use Ember.RSVP in login model? Or should I use ContainerView?
Q2: In my code, the default options of two Ember.Select(s) doesn't work, it's always the 1st option, even I set them twice in login controller and setupController. How to fix it?
Thanks