Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDavid Tonge
Courses Plus Student 45,640 PointsDid anyone get the "sortByTitle" action to work?
I passed the challenge but I realized that things in Ember(with the update) has changed, so my sort action isn't working on my local machine.
here's my posts controller code:
Blogger.PostsController = Ember.ArrayController.extend({
sortProperties: ['title'],
actions: {
sortByTitle: function(){
this.set('sortProperties',['title']);
}
}
});
and here's my posts template:
<h1>Dracula's Blog</h1>
<ul>
{{#each post in model}}
<li>{{#link-to 'post' post.id}}{{post.title}}{{/link-to}}</a></li>
{{/each}}
</ul>
<p><a href='#' {{action 'sortByTitle'}}>Sort by title</a></p>
Michael Kaiser-Nyman any idea on this?
6 Answers
Joseph Fortunato
2,270 PointsFirst, if you have the following code in your posts.js route, delete it:
controllerName: 'posts'
The default behavior for the ember route is to load a template and controller with the same name as it, so the route posts.js looks for a template named posts.hbs and a controller named posts.js, unless told otherwise. Because this line of code is specifying the name of the controller, it overrides the behavior looking for the file posts.js and looks for an ember controller named Blogger.Posts, and our controller is named Blogger.postsController. So we can go in and delete that and it will connect it to the correct controller.
Also, have to go into the posts.hbs and change this:
to this:
We do this because
is pulling the information for each post directly from the data model, not from the sorted array. If we print to the javascript console before and after we sort,
sortByTitle: function(){
console.log(this.get('firstObject'));
this.set('sortProperties', ['title']);
console.log(this.get('firstObject'));
}
you can see the sorting function is properly sorting and reordering the objects in memory, but the list we see on the screen is still in the same order as the data model, so we are not changing the original data model array. What we need to do to fix this is to go to the template and change "model" to "arrangedContent", because when we sort the array we are actually sorting the arrangedContent proxy array.
"SortableMixin works by sorting the arrangedContent array, which is the array that ArrayProxy displays. Due to the fact that the underlying 'content' array is not changed, that array will not display the sorted list" From the ember documentation
Richard Hope
25,237 PointsDavid Tonge - Did you manage to get this working. I'm having the same issue. My controller code is as follows:
Blogger.PostsController = Ember.ArrayController.extend({ actions: { sortByTitle: function() { this.set('sortProperties', ['title']); } } });
David Tonge
Courses Plus Student 45,640 PointsNaw. I didn't even get a chance to check back with with Ember. My school semester is ending soon so I should be back on it shortly.
ximena hernandez
7,326 PointsI think you do not need the assignation of "'sortProperties': ["title"]" in line2. My code works and I do not have that, I just added that line to my code and it stopped working. This is my code:
Blogger.PostsController = Ember.ArrayController.extend({ actions: { sortByTitle: function() { this.set("sortProperties", ["title"]); } } })
David Tonge
Courses Plus Student 45,640 PointsThanks for answering! I'll try this out.
Artem Syromiatnikov
4,706 PointsI've tried this out, but it doesn't seem to work for me. Double strange, as far as I'm using Ember 1.7.0, the same as in the video :(
Matthew Moyer
11,396 PointsMine wasn't working and I realized that I hadn't added <script src="controllers/posts.js"></script>
to index.html. Worked fine after I added.
Yann Vanhalewyn
18,687 PointsI have the same problem. On funny thing I noticed is if you use newer {{#each}} syntax:
{{#each post in model}}
<li>{{#link-to 'post' post.id}}{{post.title}}{{/link-to}}</a></li>
{{/each}}
The view does not get updated. Whereas using the old deprecated syntax (implicit):
{{#each}}
<li>{{#link-to 'post' this.id}}{{title}}{{/link-to}}</a></li>
{{/each}}
.. It works.. So my question is, what's the difference? The ArrayController probably isn't getting bound correctly, but I can't seem to find anything useful in Ember's documentation. (I'm using Ember 1.12.0)
Unsubscribed User
48,988 Pointsmissed the s in 'action'.......
Mavrio Design
Courses Plus Student 1,085 PointsMavrio Design
Courses Plus Student 1,085 PointsThis did it for me. Also, thanks for the great explanation!