How to proccess code in template - templates

i have a problem, i want to proccess with first element in foreach loop in template as PHP code example:
<?php
$i=0;
foreach($items as $rs){
if($i==0){
echo "first";
}else{
echo "not first";
}
}
?>
Pls help me in template Meteor. Thank you so much

I'm not too sure what you mean without some code of what you want to do, but I think you mean that you want to do something to the first item in a handlebars loop? If its that let me try and give it a shot:
This is a little tricky as the version of handlebars shipped with meteor does not yet do this so you need to do it manually.
Use index values in the transform
Template.hello.items = function() {
var i = 0;
return Items.find({}, { transform: function(doc) {
i++;
if(i==1) doc.first = true;
return doc;
}});
}
So this adds a virtual first field in your document if its the first one
Your html loop
{{#each items}}
{{#if first}}
This is the first item
{{/if}}
....
{{/each}}

Related

Get ALL Raphael IDs on a paper

Is there a way in Raphael JS to get ALL Raphael Element IDs so I can bulk update?
For example I'd like to do something like:
var allIDs = paper.getById(*);
for (var t=0; t<allIDs.length; t++)
{
paper.getById(allIDs[t]).attr({/* something */});
}
Any ideas? Thanks!
Looks like it was simpler than I thought!
paper.forEach(function (el)
{
console.log(el.id);
});

Access has Many via foreach in computed property in ember

So I've got a hasMany defined as so
quests: DS.hasMany('quest')
I have a property that is supposed to go through each quest and work out the total.
The function looks like this,
questXP: function() {
var amount = 0;
this.get('quests').forEach(function(item)
{
console.log(item.get('xpReward'));
amount += parseInt(item.get('xpReward'));
});
return amount;
}.property('quests'),
I've tried adding async: true to the hasMany but it stops the forEach from working at all. At the moment it loops 3 times(I have 3 quests) but it isn't able to access any of the quest's properties.
My thought is it's related to the fact that the quests are still being loaded.
Any ideas on what I'm doing wrong?
Your computed property depends of each xpReward property. So you need to use quests.#each.xpReward istead of quests.
questXP: function() {
var amount = 0;
this.get('quests').forEach(function(item)
{
console.log(item.get('xpReward'));
amount += parseInt(item.get('xpReward'));
});
return amount;
}.property('quests.#each.xpReward'),

Why can't I delete a property in a map function, in CouchDB?

I've got a view and a map function, in CouchDB. I'm simply iterating over some values in an array, an emitting them individually. I'd like to exclude a property of the documents that I am emitting, though. I was hoping to just call delete on it, but that doesn't appear to be doing anything. The value is still emitted with the property. Is this a limitation in order to keep indexing fast? My map function looks like this:
function(doc) {
if (doc.type == 'user' && doc.spaces) {
doc.spaces.forEach(function (space) {
if (space.tokens) {
space.tokens.forEach(function (token) {
emit(token.token, space);
});
}
});
}
}
Where I emit space, I'd like to delete the tokens property of it, before emitting. I don't want to expose other tokens in this data. So, I modified to code to look like this, with no luck. It still emits the full document, with tokens intact:
function(doc) {
if (doc.type == 'user' && doc.spaces) {
doc.spaces.forEach(function (space) {
if (space.tokens) {
var tokens = space.tokens;
delete space.tokens;
tokens.forEach(function (token) {
emit(token.token, space);
});
}
});
}
}
It happened the same trouble for me, now. And I was search the topic. But unfortunately, the other answer was not useful for me. Because, CouchDB is not SQL, documents has a different properties. Therefore I cannot emit with a specific properties.
After thinking, I hit on an idea for the problem. It is there:
function( doc )
{
// Remove a `seal` in the doc using a deep-copy technique.
var tmp = JSON.parse( JSON.stringify( doc ) );
// We can `delete` a property as we like!
delete tmp._rev;
// Emit `tmp`.
emit( null, tmp );
}
We've been bitten by this problem as well.
We worked around it by creating a new object and emitting that instead:
emit(token.token, {
name : space.name,
id : space.id
etc : etc
}
We found that was the best way anyway as most of the time there was no need to emit complete (child) documents and only what was required.

Passing parameters in Ember's Custom Events (that bubble through the View hierarchy)?

I couldn't find a way to pass arguments when using Ember's Custom Events which I found here.
I prefer avoiding solutions which target "parent" views specifically, such as this one since we lose the "bubbling".
My Usage is as following
plugins.js
Em.Object.reopen({
triggerEvent: function (eventName) {
this.$().trigger(eventName, this);
}
});
MyView.js
click: function () {
this.triggerEvent('stepClicked');
}
The code in Ember (0.96+) shows that passing an additional params is considered a manager, which isn't passed on
rootElement.delegate('.ember-view', event + '.ember', function(evt, triggeringManager) {
...
if (manager && manager !== triggeringManager) {
result = self._dispatchEvent(manager, evt, eventName, view);
} else if (view) {
result = self._bubbleEvent(view,evt,eventName);
}
Super thanks in advance,
Oren Rubin
You can pass an event object which will tell you what element is being clicked on:
click: function(event){
// will be called when when an instance's
// rendered element is clicked
console.log("element clicked: " + this.get('elementId') );
return false; // return true if you want the click event to bubble up to parent view (default it true)
}
I think an even better way would be to use {{action}} in your template. Take a look at this answer for an example.

jQuery Equal Height Columns

http://shoes4school.project-x.me/get-involved.html
Can someone please tell me what i'm doing wrong the the jQuery column faux... I can not get it to work after trying several different methods...
The code i'm using is... my url is above
$j(".content-block").height(Math.max($("#right").height(), $(".content-block").height()));
How to faux columns on webpage
(function($){
// add a new method to JQuery
$.fn.equalHeight = function() {
// find the tallest height in the collection
// that was passed in (.column)
tallest = 0;
this.each(function(){
thisHeight = $(this).height();
if( thisHeight > tallest)
tallest = thisHeight;
});
// set each items height to use the tallest value found
this.each(function(){
$(this).height(tallest);
});
}
})(jQuery);
Firebug show this error :
preloadImages is not defined
preloadImages([