a question or two on cola.js

149 views
Skip to first unread message

Decio Battaglia

unread,
Nov 2, 2013, 12:36:53 PM11/2/13
to cuj...@googlegroups.com
Hello everyone,
I am trying out cola.js (tag 0.1.5) and I have a question or two.

Some background. As an excuse for practising some sane JavaScript Web Development I am playing on an idea for a children's application for learning languages. It is something I am trying out in my limited spare time. For now I am working on a page that only has 3 buttons for selecting a current language:


I am trying out different approaches and there is also a version that does not use cola at all. For this test though, I am loading the data (an array of objects) from a module that is consumed by 'cola/adapter/Array':


The items in the collection have a status attribute. At start, all languages are inactive, but, as soon as the language_selector_controller is initialized, the default_language will be set to active. And here I have my first problem:


Is there, for this case, a more straightforward and useful way for selecting and updating an item in the collection?

Then, when the user clicks on a button:


the current language, together with the buttons, get updated. And here comes my second problem:


Here I am happy with the current language getting updated, but not with how the other languages are being set to inactive. For now this is the only solution I have managed to come up with, after looking into cola's source code... but for sure there are better ways of doing this!

Any suggestions or hints are warmly welcome. 

Thanks!

Decio

Brian Cavalier

unread,
Nov 5, 2013, 7:26:53 AM11/5/13
to cuj...@googlegroups.com
Hey Decio,

Thanks for providing code examples.

I'm not sure there's a better way to select and update the first item.  We've been intentionally keeping cola's APIs as small as possible.  Quick question: Is there a reason why you can't simply rely on the first item's status already being set to "active" when the data is loaded?  An analogy would be fetching the data from a server and relying on the server to set the appropriate item to active.

For the second issue of updating all the items, you might have a look at the forEach() method that all adapters have.  Here's an example from the cujoJS TodoMVC app where we use it to set all todos to completed:


Hope that helps a bit, and let me know about the first issue.

Decio Battaglia

unread,
Nov 5, 2013, 8:02:41 AM11/5/13
to cuj...@googlegroups.com
Thanks Brian,

I will have a look at the forEach() method and yes, I agree with you, the initial "active" state can and should be set in the data from the very beginning. My solution was more about trying out cola, rather than being sensible. :)

Thanks again!

Decio

Decio Battaglia

unread,
Nov 5, 2013, 4:50:11 PM11/5/13
to cuj...@googlegroups.com
Yes, the forEach() method worked out just fine.
:)

Brian Cavalier

unread,
Nov 5, 2013, 4:55:11 PM11/5/13
to cuj...@googlegroups.com
Great, glad to hear that worked for you!
Reply all
Reply to author
Forward
0 new messages