How to sort an array values in `desc` in computed? - sorting

I have an model value, when i do the each iteration it works fine.
<ul>
<li>See here : </li>
{{#each selectedCreditCard.balances.tenures as |balance|}}
<li>Balances is : {{balance}}</li>
{{/each}}
</ul>
But I require to sorted the value by desc way. so I use the computed method to do the desc the array.
sortTenuresBy:['desc'],
sortedTenures: Ember.computed.sort('selectedCreditCard.balances.tenures', 'sortTenuresBy'),
maxTenure:Ember.computed(function(){
return this.get('sortedTenures').get('firstObject');
But getting error as like this:
Assertion Failed: When using #each to observe the array 3,8,12,24, the array must return an object
how to fix this? please help me

If you look at API definition for Ember.computed.sort; it requires a property key (that is selectedCreditCard.balances.tenures in your case) and a sort definition (that is sortTenuresBy in your case). However, if you look at the examples provided; the sort definition must be either a plain property name or a property name followed by sort type such as name:desc or key:asc and so on. In summary; it is not possible to use Ember.computed.sort for plain arrays as in your case. I admit the API documentation is vague.
For your case; you have to either write the computed property as a function; which is what you do not want I suppose; because it is the common way; or you can make use of the following addon. What is great about ember-awesome-macros is you can nest the provided computed macros.
If you look at API for array.sort; it says "combines the functionality of both Array.prototype.sort() and Ember.computed.sort". Hence we can use this one. You want the array to be sorted in descending; I suppose something like the following
sortTenuresBy: array.reverse(array.sort('selectedCreditCard.balances.tenures'))
should work.

Related

How to get a specific value from vue.js asp.net-core-mvc result set

I've made an implementation of vue.js in an .net-core mvc project which will return data from a controller. So let's say that the result is:
[{"id":1,"uniqueStr":"string1","ttlValue":"something","hlpValue":"something2"},{"id":2,"uniqueStr":"string2","ttlValue":"something","hlpValue":"something2"}]
I can get the row value with a snippet like:
{{ data[0] }}
So I have two things remaining. The situation is, I need the rows on one page without a loop. So my question would be:
How can I get the "ttlValue" of the row where the "uniqueStr" would be "string2".
I thinking something in the line of but that obviously doesn't function:
{{ data[0][1] }}
I would do it with a computed value, like this. If you need to pass a parameter, you can use a method instead, like this.
I'm not sure what you meant with without a loop though. My solution loops through the object with .filter and with your current data structure, it's not really doable more directly.

eZPublish - how to get Selection value?

I created (in admin) a selection field called color. Now I can't access it. When I run {$note.data_map.color.content|attribute(show)} it prints value. But I can't access it without attribute(show). What can I do?
eZSelection's content is an array, access the 0 key on content to get the value.
{$node.data_map.email_option.content.0}
don't forget you always have attribute_view_gui* which can help you quite a lot in these cases.
you can set the attribute to be information collector and collect that information from user.
*{attribute_view_gui attribute=$node.data_map.color}
You need to match the option array defined in your class attribute with the id of the selected option in order to get the value of it.
$node.data_map.color.class_content.options will contain all the options available (associative array with id and name values)
$node.data_map.color.content is an array containing the ids of the selected options (because this field can handle multiple selection).
Even if the {section} function is deprecated I'll suggested that you have a look at the default template rendering an ezselection attribute : design/standard/templates/content/datatype/view/ezselection.tpl
If you have "Multiple choice" type than you can do it like this:
{if $node.data_map.color.has_content}
{foreach $node.data_map.color.content as $colorID}
{foreach $node.data_map.color.class_content.options as $opt}
{cond($opt.id|eq($colorID), $opt.name, '')}
{/foreach}
{/foreach}
{/if}

How do I restrict the number of characters returned from a query using Sequel and Ruby?

My model is Article, and I want to display just a snippet of the body of the article, to give a preview of this particular article.
How would I do that with Sequel?
I thought it might be using limit, but that just limits the number of records returned from the db.
Ideally I would like to do something like: Article.first.limit(40) where 40 is the first 40 characters.
I know limit won't work, but I am just using it as an example to illustrate what I am looking for.
Edit 1:
I am using Sinatra. I wanted to restrict the returned values but from the view, rather than the routing file.
I do something like this in the routing file:
#section = HelpSections.filter(:type => 'a').order(:sort, :name)
Which gives me a list of all sections of type a.
Then, to get to an article I do:
#section.each do |article|
article.question.each do |title|
title.name[0..9]
end
end
Based on this scenario, what I want to do is restrict the size of the 'name' attribute returned.
But, when I do that, I get this error:
undefined method '[]' for nil:NilClass
How do I handle this, so that I can restrict the size of the name attribute?
Could you not just do something like this?:
Article.first[:body][0..39]
If you really need to do it through SQL, you can do something like this:
Article.select {|a| a.substr(:body, 1, 40) }
(You may have to use .substring in place of substr depending on your DB)

Rails: Generating Acts_as_Taggable_On tag counts from collection of tagged objects

I have a site with Thinking Sphinx set up to return search results for Product names, tags, descriptions, etc. Works fine.
However, when I get a group of search results back from TS, I get a "no method" error when trying to get tag_counts from Acts_as_Taggable_On. I've found that the error extends to a range of situations where I might call tag_counts method on a collection of tagged items.
These work:
Owner.first.products.all.tag_counts
Product.where(:color => 'white').tag_counts
Product.first.tag_counts
But these don't:
Product.all.tag_counts
Product.search('white').to_a.tag_counts
(The latter calls a Thinking Sphinx search that returns a TS search collection.)
They come back with some variant of this error:
NoMethodError: undefined method `tag_counts' for #<Array:0x00000101585280>
I have a vague idea this is some kind of proxy method thing where it's getting properly associated on the former calls but not the latter.
Anyone have suggestions on how I can ensure that an array of tagged objects has the tag_counts method available?
Product.all returns an array and you're trying to run the method tag_counts on that array which won't work, as the array won't have that method.
What you're probably wanting to do is:
Product.tag_counts
It should return the tag counts for all products.

Duplicated Zend_Form Element ID in a page with various forms

How do I tell the Zend_Form that I want an element (and it's ID-label, etc) to use another ID value instead of the element's name?
I have several forms in a page. Some of them have repeated names. So as Zend_Form creates elements' IDs using names I end up with multiple elements with the same ID, which makes my (X)HTML document invalid.
What is the best solution to fix this, given that I really have to stick with using the same element names (they are a hash common to all forms and using the Zend_Form Hash Element is really out of question)?
Zend_Form_Element has a method called setAttribs that takes an array. You may be able to do something like $element->setAttribs(array('id' => "some_id"));
or you can do $element->setAttrib('id', 'some_id');
Thanks, Chris Gutierrez.
However, as I said, I needed to get ride of the default decorator generated IDs like -label. Wiht the $element->setAttribs() it is not possible, however.
So based on http://framework.zend.com/issues/browse/ZF-7125 I just did the following:
$element->clearDecorators();
$element->setAttrib('id', 'some_id');
$element->addDecorator("ViewHelper");
Whoever sees this: please note this was enough for what I needed. But may not be for you (the default settings has more than the viewHelper decorator).

Resources