A way to know what iteration we’re in at in an Each would be nice. For instance a Count, First and Last parameter where Count is an int representing the iteration. And First and Last would be bools representing whether we’re in the first or last iteration.
Sorry, I read that to fast. Thought you where refering to Observable.forEach. I think we have some tickets regarding what you are asking about. I will take a look. But you can always just modify the elements array with the properties you need in UX, like adding an index.
It’s also a bit unfortunate that JS doesn’t have a simple way to clone objects; in that example you’re adding count and i fields to the incoming objects and returning those instead of returning new ones. Which is probably ok, but it’s important to be aware of. I can imagine some really nasty problem cases arising this way.
Ah, anders basically said that, and I guess bolav wrote the example with that in mind, so what I said is basically superfluous Probably good to have the reminder tho, hehe.
Another suggestion might be to build a “wrapper” object in map with the extra data you need and “unwrap” it in Each using Select. Then you don’t have to feel icky because you’re not “polluting” your original data . In that case, bolav’s example can be modified like this:
JS:
var elements = elem_text.map(function (x, i) {
return {
element: x,
max_index: elem_text.count() - 1,
index: i
};
});
I haven’t tested this code, but it should work just fine. Note that I think the reason why bolav had an issue with binding to count was actually because the index would never reach that value, being 0-indexed. I’ve [hopefully] fixed this and renamed the count field to max_index in this example to illustrate the difference
The issue seems to be that you cannot reference data sources with <Case>. The below code is pretty much identical to Jake’s example (see above) but it generates the following error:
Object reference not set to an instance of an object. System.NullReferenceException occured.
The code used to get the above error is pasted below:
MainView.js
var Observable = require("FuseJS/Observable");
var elem_text = Observable(
{"text": "hei"},
{"text": "hade"}
);
var elements = elem_text.map(function (x, i) {
return {
element: x,
max_index: elem_text.count() - 1,
index: i
};
});
module.exports = {
elements: elements
};