How to remove elements from an Ember.ArrayController - ember.js

In JavaScript, one can remove selected elements from an array by traversing it in reverse order and using splice(index, 1) to remove undesired elements. I'm trying to figure out how to do the same thing in Ember.js (without Ember Data).
I have an ArrayController and the associated Route's model function simply returns a JavaScript array. There an action in the controller, along the following lines:
removeElements: function () {
var i, arr = this.get('content'),
i = arr.length;
while (i) {
i -= 1;
if (arr[i].get('flag')) {
array.replace(i, 1);
}
}
This first appears to work in the browser. For example, if I have three elements and mark the first and third to be removed, the browser will leave the second item displayed. However, if I later try to mark the latter, Ember complains with Uncaught Error: Can't remove an item that has never been added.
I used replace() because Ember arrays don't have a splice method, but the docs also say that replace must be implemented in order to be used, but I don't quite understand where I'm supposed to implement it and I haven't found any sample implementations to guide me.
I've also tried various other methods, such as removeObject, removeObjects and more, but none did what I need.

You'd need to create your own array collection and implement replace on that collection (probably extending Ember.Array to get you started).
removeObject should work just fine (granted a little inefficient, though if the size of this list is small it's negligible):
removeElements: function () {
var controller = this,
list = this.toArray();
list.forEach(function(item){
if(item.get('flag')){
controller.removeObject(item);
}
});
}
using removeAt should give you the results you're looking for
removeElements: function () {
var i = this.get('length');
while (i--) {
if (this.objectAt(i).get('flag')) {
this.removeAt(i);
}
}
}

Related

Cracking open results returned by Ember peekAll

I'm playing around with peekAll(), trying to understand how it works for the ultimate purpose of iterating through the results.
In a route's model hook, I have:
var peekAllResults = this.store.peekAll('position');
console.log("peekAllResults = ", peekAllResults);
var peekAllResultsContent = peekAllResults.get('content');
console.log("peekAlLresultsContent = ", peekAllResultsContent);
This is returning data, as expected based on what I've got in my app.
In particular, here's what shows in the console:
So far so good. There are 8 records as expected based on what I've got going on.
But when when I add:
console.log("peekAllResultsContent.length=", peekAllResultsContent.length)
I get: peekAllResultsContent.length = 0
Same thing if I do peekAllResultsContent.get("length")
What is going on there?
I thought peekAll was a synchronous call that returned an array. Is there some trick to cracking it open and seeing what's actually in the array? I can't even get the length, so I figure I'm not on the right track.
Everything is wrapped into Ember.Model objects so you won't see clear results from console.log.
But there is no magic behind it. If the entities are already loaded into store you can get them via peekAll.
const positions = this.get('store').peekAll('position');
console.log('positions length', positions.get('length');
//we can iterate over them:
positions.forEach(position => {
console.log(position.get('name'));
};
//we can filter them:
const southOnlyPositions = positions.filter(position => position.get('direction') === 'south');
and so on...
Btw: even for promises you are not supposed to access content. You get the result like this:
const promises = this.get('store').findAll('position');
promises.then(positions => {
// positions here behave same as before
});

How to write a boundHelper to iterate a map in Ember.js

I have a model which contains an Ember.Map, and I want to render the content of that map in a template.
I've tried using the custom bound helper below, but the template will not re-render as values are added/removed from the map.
Essentially I just want to replicate the behaviour of {{#each}} for a map.
Ember.Handlebars.registerBoundHelper('eachInMap', function(map, block) {
out = "";
map.forEach(function(k,v) {
out += block.fn(v)
});
return new Handlebars.SafeString(out);
}, /* what dependencies to put here? */);
Invoked by a template
{{#eachInMap myMap}} foo bar {{/eachInMap}}
Check out https://github.com/emberjs/ember.js/pull/2659.
Basically, boundHelpers don't currently support blocks sorry.
The current workaround is to create a non-bound helper and wrap it in a {{#bind}} block.

PlayFramework 2.0 - Not able to call functions from other templates

I want to place some helper functions in another file, since they will be overly reused. I took the Computer-Databse sample's listing file:
https://github.com/playframework/Play20/blob/master/samples/scala/computer-database/app/views/list.scala.html
I created a new file, called "listing.scala.html" under the app/views package, and moved the #link function from the original file to it. This new file looks like this:
#(currentSortBy: String, currentOrder: String, currentFilter: String)
#****************************************
* Helper generating navigation links *
****************************************#
#link(newPage:Int, newSortBy:String) = #{
var sortBy = currentSortBy
var order = currentOrder
if(newSortBy != null) {
sortBy = newSortBy
if(currentSortBy == newSortBy) {
if(currentOrder == "asc") {
order = "desc"
} else {
order = "asc"
}
} else {
order = "asc"
}
}
// Generate the link
routes.Application.listPerfil(newPage, sortBy, order, currentFilter)
}
So, on my original file, I replaced the #link call, with this one:
#title
And the problem is, when I try to compile I get this error:
value link is not a member of play.api.templates.Html
But according to the documentation (http://www.playframework.org/documentation/2.0.4/ScalaTemplateUseCases) it seems to be ok.
Any guess?
Play's templates aren't the best place for placing advanced conditions, most probably you'll get better flexibility by processing it in some controller (or other method) which will return you only required link
ie.:
#title
In your case proposed link(...) function of Application controller can also return a reverse-route.
Keep in mind that including other templates is best option for repeating blocks of HTML but sometimes it's hard to get specified string (mainly because of not trimmed spaces). As you can see there is also problem with calling nested functions. Most probably you can generate whole A tag in the listing.scala.html however using it isn't comfortable enough (IMHO).

How would I modify this ember.js function to return an Enumerable or Array

I have just begun learning ember.js, I have followed some tutorials and created a working example here:
App.Track.reopenClass({
find: function() {
var tracks = [];
$.ajax({
url: 'http://ws.spotify.com/lookup/1/.jsonuri=spotify:album:6J6nlVu4JMveJz0YM9zDgL&extras=track',
dataType: 'json',
context: this,
success: function(data, textStatus, jqXHR) {
$.each(data.album.tracks, function(index, value) {
track_id = value.href.replace("spotify:track:", "");
tracks.addObject(App.Track.create(value));
// I would rather do something like:
// tracks[track_id] = App.Track.create(value)
});
}
})
return tracks;
}
});
This function hits an API and loops through the returned data to populate the tracks object (tracks.addObject(App.Track.create(value));) and return it.
Rather than getting an ordinary object back from this function, I would like to get an Enumerable / Array so I can manipulate it with filterProperty or pull out tracks by id (There is a track_id which I would like to use as the array index).
All of my attempts to use an array have broken ember's magical ability to update the view when the ajax call populates the tracks.
Can anyone modify http://jsfiddle.net/ZEzwn/ to return an Enumerable (preferably an Array) but still update the view automatically?
As your method already returns an Array (because you have Ember prototype extension enabled), doing:
var tracks = [];
is equivalent to
var tracks = Ember.A();
On ajax request success, you're just populating the array, so you could use Ember.Array methods like filterProperty.
Just one thing about using id as array key, you really SHOULD NOT, as Ryan Bigg says in its blog:
However, if the variant’s id is [something a little higher, like] 1,013,589,413, then you start to run into problems.
In that case, JavaScript would create a one billion, thirteen million, five hundred and eighty-nine thousand, four hundred and fourteen element array. All to store one value in, right at the end.
Ok this is now working, as louiscoquio pointed out, tracks IS an enumerable object and I can do stuff like
tracks.filterProperty('href', 'spotify:track:7x7F7xBqXqr0L9wqJ3tuQW')
tracks.getEach('name')
tracks.get('firstObject')

Ignore or replace Handlebars blocks that don't compile

In a handlebars template in Ember.js, I have blocks like the following:
{{content.some_attribute}}
{{content.some_other_attr}}
{{content.more_attr}}
Some of these attributes don't exist and I'm implementing them slowly.
Is there a way to get these templates to compile and either ignore the blocks that don't evaluate or better yet, replace them with a html element so they're easier to spot in the browser?
(the template is pretty large and it's being converted from ERB slowly,
Is there a way to get these templates to compile and either ignore the blocks that don't evaluate
Properties that don't exist are undefined, and don't get rendered at all. In other words {{thisDoesNotExist}} will simply be invisible -- it will compile just fine.
or better yet, replace them with a html element so they're easier to spot in the browser
As Cory said, you could use a helper for this that checks for undefined, using Ember.Handlebars.registerBoundHelper.
This seems like a perfect case for a handlebars helper. The helper could validate the value and return the value or the html that you desire.
The following code should be used very carefully, since it has not been tested within an application.
A possible solution to replace the possible undefined value in a template is to overwrite Ember.getPath, which is used to lookup the value of a path in a template, see http://jsfiddle.net/pangratz666/hKK8p/:
var getPath = Ember.getPath;
Ember.getPath = function(obj, path) {
var value = getPath(obj, path);
return (Ember.none(value) ? 'OMG %# is not defined!!'.fmt(path) : value);
};
If this code would be used temporarily in an application, I would also restrict the check for undefined values to a specific obj. So something along those lines:
App.objectWhichHasUndefinedProps = Ember.Object.create({
...
});
Ember.View.create({
templateName: 'templateWithAttributes',
objBinding: 'App.objectWhichHasUndefinedProps'
}).append();
var getPath = Ember.getPath;
Ember.getPath = function(obj, path) {
var value = getPath(obj, path);
if (obj === App.objectWhichHasUndefinedProps) {
return (Ember.none(value) ? 'OMG %# is not defined!!'.fmt(path) : value);
}
return value;
};