Backbone underscore sum value - backbone.js

I have an app in backbone and I want to know if is possible to sum inside a template value of the object.
For example I have this piece of template in underscore:
<% _.each(room1.combinations, function(room2) { %>
<div>
<div class="tot"><p>TOTAL:<span id="totale_<%= room2[0].attributes.id %>"></span></p>
</div>
<form method="POST" action="">
<% _.each(room2, function(room) { %>
<span><%= room.attributes.price %> EUR</span>
<% }); %>
<input type="button" class="submit-ricerca prenota-bt" name="buy" value="BUY">
</form>
</div>
<% }); %>
I want to put into the span with class total the sum of the price of each element inside it.
Is possible?
Thanks

Yes, it is possible. Just sum the prices (using reduce) and put them there:
<p>TOTAL:<span id="totale_<%= room2[0].attributes.id %>"><%=
_.reduce(room2, function(sum, room){return sum+room.attributes.price;}, 0)
%></span>

Related

How to mark the simple_form checkbox based on a Array object?

The Rails view has two parameters
list_of_attributes = ["a1", "a2", "a3", "a4"]
user_selections = ["a2", "a4"]
I am able to display the appropriate checkboxes and any associated user selections using the following simple_form definition
<% list_of_attributes.each do |item| %>
<label>
<%= check_box_tag "user_selections[]", item, user_selections.include?(item) %>
<%= item %>
</label>
<% end %>
How can I define the above behavior using simple_form f.input syntax? With the following definition, I am able to display the appropriate checkboxes, but any user selections is not 'checked'.
<%= f.input key, as: :check_boxes, collection: list_of_attributes,
:label => key, class: "form-control" %>
Thanks for your help.
Adding checked attribute got the f.input definition functional.
<%= f.input key, as: :check_boxes,
collection: list_of_attributes,
:label => key,
checked: user_selections,
class: "form-control" %>
<% end %>
Try this. This worked for me using bootstrap CSS. Check line 25 in my github repo here
<div class="form-group">
<div class="row">
<div class="col-sm-offset-3 col-sm-8">
<%= f.collection_check_boxes :your_model_ids, Your_model.all, :id, :list_of_attributes do |cb| %>
<% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text} %>
<% end %>
</div>
</div>
</div>
And then use this in your CSS file for the custom input_checkbox class above
.input_checkbox input {
width: auto !important;
}

Why is fields_for rendering different indexes in the same form?

I’m using Rails 4.2.3. In my view, I make use of the “fields_for” directive. Notice I use it twice (“f.fields_for :my_object_times”) below …
<div class="field">
<%= f.label "Time" %> <span class="required">*</span><br>
<%= select_tag('my_object[hour]', options_for_select((0..99).to_a.map { |i| i.to_s.rjust(2,'0')}), {:prompt => 'Select Hour'} ) %> hrs
<%= select_tag('my_object[minute]', options_for_select((0..60).to_a.map { |i| i.to_s.rjust(2,'0')}), {:prompt => 'Select Minutes'} ) %> min
<%= select_tag('my_object[second]', options_for_select((0..60).to_a.map { |i| i.to_s.rjust(2,'0')}), {:prompt => 'Select Seconds'} ) %> sec
<%= f.fields_for :my_object_times do |mot| %>
<%= mot.hidden_field :time_in_ms %>
<% end %>
</div>
<div class="field">
<%= f.fields_for :address do |addr| %>
<%= addr.label :address %><br>
City: <%= addr.text_field :city %>
<%= select_tag :state, options_for_select(us_states) %>
<%= country_code_select(:country, :country_id,
nil,
{:include_blank=>false},
{:style=>''}
) %>
<% end %>
</div>
<%= f.fields_for :my_object_times do |rt| %>
<div class="field">
<%= rt.label :overall_rank %><br>
<%= rt.text_field :overall_rank %>
</div>
<div class="field">
<%= rt.label :age_group_rank %><br>
<%= rt.text_field :age_group_rank %>
</div>
<div class="field">
<%= rt.label :gender_rank %><br>
<%= rt.text_field :gender_rank %>
</div>
<% end %>
But when my form gets rendered, the array index on the attributes are displayed differently (one is “0” and the other is “1”) …
<input type="hidden" name="my_object[my_object_times_attributes][0][time_in_ms]" id="my_object_my_object_times_attributes_0_time_in_ms" value="252008000">
…
<input type="text" name="my_object[my_object_times_attributes][1][overall_rank]" id="my_object_my_object_times_attributes_1_overall_rank">
How do I modify my view code so that both attributes get rendered with the “0” index? Note I would prefer to leave the order of my elements on my page exactly as they are.
Two solutions come to mind:
1 - Group your hidden_field (since it's not being shown) with your second form_for which will make it a non-issue and be more elegant.
2 - build your my_object_times object in your controller so when you call the fields_for method you can refer that object which should (I haven't tested it) give it the same reference.
If none of the two options are possible, I'd look at the code and see if there's any way you can refactor to make it work.

How to remove item from nested array in partial view in angularjs?

I have recursive partial in angular for showing comments threads. And I have controls for edit and delete. Deleting working on server but i can't update client.
My main page:
<div class="list-group-item" ng-repeat="ch in img.comments" ng-include="'views/nestedComment.html'">
</div>
My partial look like:
<div class="list-group-item-info">
<h4>{{ch.app_user.email}}:</h4>
{{ch.body}}
<div class="controls" ng-if="ch.app_user_id==user.getUser().id">
<%=t :edit %>
<%=t :destroy %>
<%=t :answer %>
<%=t :shownested %>
<div class="list-group-item-danger" ng-repeat="ch in ch.children" ng-show="nested" ng-include="'views/nestedComment.html'">
</div>
</div>
</div>
And I use this function to delete
$scope.deletenested=function(parent,item){
$scope.$evalAsync(function(){
parent.children.splice(parent.children.indexOf(item),1);
})
$http.delete('/comments/'+item.id)
.success(function(){
alert('OK')
parent.children.splice(parent.children.indexOf(item),1)
})
.error(function(response,status){
if (status=='401'){
alert('You have to autorize')
$window.location.href='http://localhost:3000/app_users/sign_in';
}
})
}
This worked for me when i didn't use partials. But now i can't access to parent array.

How do i nest div elements in HTML?

is there a possibility to code a loop in HTML to nest div elements in that way(the child elements are the interesting ones):
<div class="parent">
<div class="child">
<div class="child">
<div class="child">...
I don't know the main language of your project, but a simple solution that comes to mi mind and think it works is doing something like this:
<div class="parent">
<% simple for statement in your main languaje %>
<div class="child">
<% end %>
<% same simple for statement in your main languaje %>
</div>
<% end %>
</div>

SilverStripe: Loop If Statement

I'm building a single page portfolio with SilverStripe.
So far I am able to loop through my sites and all $Title[s] and $Content[s] are visible in one <div>. Now I want to attach a certain CSS-class if the current page (which is looped) is named "Contact".
Something like:
//Pseudocode:
<loop start>
if ($Title == 'Contact') <div class="a"></div> else <div class="b"></div>
<loop end>
Does anybody know how to do this?
<% if $Title == "Contact" %>
// Do something ...
<% end_if %>
you could try a custom getter method such as :
function DivClassName() {
return $this->Title == 'Contact'?'a':'b';
}
and use the following in your template.
<div class="$DivClassName"></div>
keeps the logic out of your templates :)
<% if Title = "Contact" %>
<div class="a"></div>
<% else %>
<div class="b"></div>
<% end_if %>

Resources