Ember.js - Binding to another controller's model - ember.js

In my project, I have the following three models:
Club Model: represents a sports club. It has one clubMeta and many customCourts.
ClubMeta Model: club related information relevant to the app.
CustomCourt Model: belongs to club. A club should have customCourts only if clubMeta.customCourtsEnabled is true.
Club
var Club = DS.Model.extend({
... bunch of properties ...
clubmeta: DS.belongsTo('App.Clubmeta'),
customCourts: DS.hasMany('App.CustomCourt')
});
module.exports = Club;
Clubmeta
var Clubmeta = DS.Model.extend({
... bunch of properties ...
customCourtsEnabled: DS.attr('boolean'),
club: DS.belongsTo('App.Club')
});
module.exports = Clubmeta;
CustomCourt
var CustomCourt = DS.Model.extend({
name: DS.attr('string'),
glass: DS.attr('boolean'),
indoor: DS.attr('boolean'),
single: DS.attr('boolean'),
club: DS.belongsTo('App.Club')
});
module.exports = CustomCourt;
What I need to do is a template where the club (which is the logged in user) can add customCourts only if clubmeta.customCourtsEnabled is true. As I was told in another SO question, I should be using an ArrayController to handle CustomCourts.
Everything is ok until this point, the problem comes because CustomCourtsController needs to know about club and clubmeta. I have tried this, with some variations to the binding path:
var ClubCourtsController = Ember.ArrayController.extend({
needs: ['currentClub'],
customCourtsEnabledBinding: Ember.Binding.oneWay("App.currentClubController.content.clubmeta.customCourtsEnabled"),
...
});
CurrentClubController
var CurrentClubController = Ember.ObjectController.extend({
init: function() {
this._super();
console.log('Retrieving club ' + App.clubId);
this.set('content', App.Club.find(App.clubId));
}
});
module.exports = CurrentClubController;
But ClubCourtsController.customCourtsEnabled always returns undefined. What is the right way to do this?

ClubCourtsController.customCourtsEnabled always returns undefined. What is the right way to do this?
You're on the right track. For starters the right way to do the binding is via the controllers property:
var ClubCourtsController = Ember.ArrayController.extend({
needs: ['currentClub'],
customCourtsEnabledBinding: Ember.Binding.oneWay("controllers.currentClub.clubmeta.customCourtsEnabled"),
});
Beyond that, it's generally best practice to wire things together in your route's setupController hook instead of a controller init. So even if you've got that binding right, could be other issues causing the customCourtsEnabled property to be undefined. I've posted a working example on jsbin here: http://jsbin.com/ubovil/1/edit
App = Ember.Application.create({
clubId: 1
});
App.Router.map(function() {
this.route("customCourts", { path: "/" });
});
App.Club = Ember.Object.extend({});
App.Club.reopenClass({
find: function(id) {
return App.Club.create({
id: id,
clubmeta: App.ClubMeta.create({
customCourtsEnabled: true
})
});
}
});
App.ClubMeta = Ember.Object.extend({});
App.CurrentClubController = Ember.ObjectController.extend({});
App.ApplicationController = Ember.Controller.extend({
needs: ['currentClub']
});
App.ApplicationRoute = Ember.Route.extend({
setupController: function(controller) {
controller.set('controllers.currentClub.content', App.Club.find(App.clubId));
}
});
App.CustomCourtsController = Ember.ArrayController.extend({
needs: ['currentClub'],
customCourtsEnabledBinding: Ember.Binding.oneWay("controllers.currentClub.clubmeta.customCourtsEnabled")
});
App.CustomCourtsRoute = Ember.Route.extend({
setupController: function(controller) {
controller.set('content', ['Court a', 'Court b', 'Court c']);
}
});

You need to access the controller via the controllers property
ClubCourtsController = Ember.ArrayController.extend({
needs: ['currentClub'],
customCourtsEnabledBinding: Ember.Binding.oneWay("controllers.currentClub.clubmeta.customCourtsEnabled"),
...
});

Related

How to come up with the global path to an Ember controller's property when value binding to a view helper?

Let's say I have the following controllers
App.PersonController = Ember.ObjectController.extend();
App.PersonStuffController = Ember.Controller.extend({ somethingOnController: [] });
with router entries
this.resource('person', function() {
this.route('stuff');
});
and routes
App.PersonRoute = Ember.Route.extend({
model: function() {
this.store.createRecord('person', {});
},
setupController: function(controller, model) {
controller.set('model', model);
}
});
App.PersonStuffRoute = Ember.Route.extend({
model: function() {
this.modelFor('person');
},
setupController: function(controller, model) {
controller.set('person', model);
this.store.find('thing').then(function(things) {
controller.set('things', things);
});
}
});
and models
App.Thing = DS.Model.extend({
name: DS.attr('string'), // Instances of this model will have name values that match a Person instance's property keys
personStuffPath: function() {
return 'person.' + this.get('name');
}.property('name')
});
App.Person = DS.Model.extend({
// A lot of attributes that match the names of App.Thing instance name property values
});
In my template for PersonStuff, I have code
{{#each thing in things}}
{{view Ember.Select content=somethingOnController value=thing.personStuffPath.value}}
{{/each}}
So what I'm expecting here is a bunch of input fields that are bound to PersonStuffController's person property's properties. Instead I get the error:
Uncaught Error: Assertion Failed: Path 'person.exampleThingName' must be global if no obj is given.
So I modified my Thing model to instead be:
App.Thing = DS.Model.extend({
name: DS.attr('string', { defaultValue: 'thingName' }),
personStuffPath: function() {
return 'App.PersonStuffController.person.' + this.get('name');
}.property('name')
});
and the error instead becomes:
Uncaught Error: Property set failed: object in path "App.PersonStuffController.person" could not be found or was destroyed.
This should be the right global path, right? Can I just not do things this way for a reason?
JSBins:
Relative path error: http://jsbin.com/lozayobe/1/edit?html,js,output
Global path error: http://jsbin.com/lozayobe/2/edit?html,js,output
App.FooController refers to the class, it's not the actual instance. In most cases Ember controllers and routes are singletons, so they aren't recreated (except item controllers). If you wanted to create a global way of accessing a controller you would need to get the controller then add it to some global namespace.
For example (I'm not necessarily recommending this, but it's good to understand how things work):
App.ApplicationController = Em.Controller.extend({
beforeModel: function(){
var fooInstance = this.controllerFor('foo');
App.FooControllerInstance = fooInstance;
}
});
Now App.FooControllerInstance has the instance of FooController where you can map dynamic computed properties.

Emberjs promiseArray inside route doesn't return properly

I have a controller for showing item.
Users can put the item in their wish list.
(Item has many users, User has many Items.)
So, when user enter the webpage, I want to show a AddToList or RemoveFromList button to the user based on isAddedToList property.
Below is the code.
User Model:
var User = DS.Model.extend({
username: DS.attr('string'),
email: DS.attr('string'),
avatar: DS.attr('string'),
items: DS.hasMany("item", { async: true })
});
export default User;
ItemModel:
var Item = DS.Model.extend({
name: DS.attr("string"),
priceInCent: DS.attr("number"),
salePriceInCent: DS.attr("number"),
brand: DS.belongsTo("brand"),
itemImages: DS.hasMany("itemImage", { async: true }),
users: DS.hasMany("user", { async: true }),
});
export default Item;
ItemRoute:
var ItemRoute = Ember.Route.extend({
model: function(params) {
var userId = this.get("session").get("userId");
return Ember.RSVP.hash({
item: this.store.find('item', params.item_id),
user: this.store.find('user', userId),
});
},
setupController: function(controller, model) {
controller.set('item', model.item);
controller.set('user', model.user);
}
});
export default ItemRoute;
ItemController:
var ItemController = Ember.Controller.extend({
needs: ["current-user", "application"],
currentUser: Ember.computed.alias("controllers.current-user"),
isAddedToList: function() {
var promiseUsers = this.get("item.users"), user = this.get("user");
return promiseUsers.contains(user);
}.property("item"),
actions: {
addToList: function() {
var item = this.get("item"), user = this.get("user");
item.get("users").pushObject(user);
item.set("addedUserIds", [user.get("id")]);
item.save();
},
removeFromList: function() {
var item = this.get("item"), user = this.get("user");
item.get("users").removeObject(user);
item.set("removedUserIds", [user.get("id")]);
item.save();
}
}
});
export default ItemController;
The problem is when I check the length of promiseUsers with
promiseUsers.get("length")
it always returns 0.
but when I try the same with Chrome console, it returns the length properly.
Do I miss something in the route? How to fix the problem?
The problem is you're using your code synchronously, despite it being an asynchronous property.
The first time you attempt to use an async relationship it will begin resolving the relationship, making a callback to the server is necessary. In your case you try to use the users right away, but they are going to be empty the first time, so you're contains will return false. Since you aren't watching the users' collection it will then update, but the computed property won't update since the computed property was just watching item. This is why when you try it from the console it works, because by the time you attempt to use it in the console it's finished resolving the async collection of users.
isAddedToList: function() {
var promiseUsers = this.get("item.users"), user = this.get("user");
return promiseUsers.contains(user);
}.property("user", 'item.users.[]')

Recognize when loading of hasMany relationship is finished

I've got two models, connected with a async hasMany relationship:
App.Car = DS.Model.extend({
model: DS.attr('string'),
passengers: DS.hasMany('passenger', { async: true }
});
and
App.Passenger = DS.Model.extend({
name: DS.attr('string')
});
How can I observe the loading status of a cars passenger? I tried:
App.CarPassengerController = Em.ObjectController.extend({
needs: ['cars'],
carsBinding: 'controllers.cars',
loadObserver: function() {
console.log('passengers loading status of changed');
}.observes('cars.#each.passengers.isFulfilled')
});
This doesn't work, as the observer is only fired after the cars are loaded, but not after the promises are fulfilled. (I know they are fulfilled, because this is indicated in the Ember Inspector.)
Has anyone experienced a similar problem and found a solution for it? Thanks!
There's gotta be a better way than this (and I'd be interested in knowing too), but this is how I've been doing it (hacking it, really) so far:
setupController: function(controller, model) {
this._super(controller, model);
var cars = model.get('cars')
var carCount = cars.get('length')
var carIterator = 0
cars.forEach(function(car){
car.get('passengers').then(function(passengers){
carIterator++;
if(carIterator == carCount) {
// all passengers from all cars have been loaded
controller.set('passengers_loaded', true)
}
})
})
}

Implementing model filter

I have set up the following scaffolding for my Ember application.
window.App = Ember.Application.create({});
App.Router.map(function () {
this.resource('coaches', function() {
this.resource('coach', {path: "/:person_id"});
});
});
App.ApplicationAdapter = DS.FixtureAdapter.extend({});
App.Person = DS.Model.extend({
fname: DS.attr('string')
,lname: DS.attr('string')
,sport: DS.attr('string')
,bio: DS.attr('string')
,coach: DS.attr('boolean')
,athlete: DS.attr('boolean')
});
App.Person.FIXTURES = [
{
id: 10
,fname: 'Jonny'
,lname: 'Batman'
,sport: 'Couch Luge'
,bio: 'Blah, blah, blah'
,coach: true
,athlete: true
}
,{
id: 11
,fname: 'Jimmy'
,lname: 'Falcon'
,sport: 'Cycling'
,bio: 'Yada, yada, yada'
,coach: false
,athlete: true
}
];
I am trying to set up a route to filter the person model and return only coaches. Just to make sure I can access the data, I have simply used a findAll on the person model.
App.CoachesRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('person');
}
});
Now though, I am trying to implement the filter method detailed on the bottom of the Ember.js Models - FAQ page.
App.CoachesRoute = Ember.Route.extend({
model: function() {
var store = this.store;
return store.filter('coaches', { coach: true }, function(coaches) {
return coaches.get('isCoach');
});
}
});
The coaches route is not working at all with the new route implemented and the old one commented out. I am using the Ember Chrome extension and when using the filter route the console responds with, Error while loading route: Error: No model was found for 'coaches'. Apparently the route is not working, specifically the model. No kidding, right? What am I missing in my filter model route?
Thank you in advance for your help.
The error message is spot on- there is no CoachModel. I think you need to do this:
App.CoachesRoute = Ember.Route.extend({
model: function() {
var store = this.store;
return store.filter('person', { coach: true }, function(coaches) {
return coaches.get('isCoach');
});
}
});

Ember.js access model values

I'd like to be able to modify/validate data before actually saving.
Model
App.Post = DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
date: DS.attr('date', { defaultValue: new Date() }),
excerpt: DS.attr('string'),
body: DS.attr('string')
});
Route
App.PostsNewRoute = Ember.Route.extend({
model: function() {
return this.get('store').createRecord('post');
},
actions: {
doneEditing: function() {
debugger;
this.modelFor('postsNew').save();
this.transitionTo('posts.index');
}
}
});
So, the questions, before the .save() I want to, let's say, validate that the title is not empty or so.
Everything I've tried gets undefined, or [Object object] has no .val() method. I don't know how to get to the values of the model. How can I do that?
And the other thing I have in mind. Is that defaultValue working as intended? I want to set Date() to every new created post. Somehow date is not being recorded since it's not showing.
Thanks.
App.PostsNewRoute = Ember.Route.extend({
model: function() {
return this.get('store').createRecord('post');
},
actions: {
doneEditing: function() {
debugger;
var model = this.modelFor('postsNew');
var title = model.get('title');
model.save();
this.transitionTo('posts.index');
}
}
});