apply ko.observable to an entire object

3,390 views
Skip to first unread message

ceej

unread,
Aug 2, 2011, 5:21:11 PM8/2/11
to KnockoutJS
Hi,

What's the best way to apply ko.observable to everything in an entire
object and it's nested values?

Many thanks

ceej

unread,
Aug 2, 2011, 5:38:47 PM8/2/11
to KnockoutJS

rpn

unread,
Aug 2, 2011, 10:23:02 PM8/2/11
to knock...@googlegroups.com
Have you had a look at the mapping plugin?  By default it will basically make everything observable.

ceej

unread,
Aug 3, 2011, 10:29:35 AM8/3/11
to KnockoutJS
Would you say using the mapping plugin is better/faster than doing
https://gist.github.com/af25b8e5e73ee1ea4540 ?

On Aug 2, 9:23 pm, rpn <rnieme...@gmail.com> wrote:
> Have you had a look at the mapping plugin<http://knockoutjs.com/documentation/plugins-mapping.html>?

ceej

unread,
Aug 3, 2011, 11:06:26 AM8/3/11
to KnockoutJS
So I'm trying it out, I can't add each object to a t object like
t.flag_red to avoid things like "flag_red is not defined" but that
probably wasn;t the best way to do it.... I'm using coffeescript
templates like https://gist.github.com/7bc623bfa9d14f8508b4 surely
theirs a template method that I can extend and add something like if
typof variable is 'undefined' or _.isNull(variable) then return false
else variable which would avoid the is not defined errors.....

ceej

unread,
Aug 3, 2011, 11:28:46 AM8/3/11
to KnockoutJS
so I can do var html = _.template(tmpl)({t: data}); but I'd really
like to add it on the mapping level so updates etc... work plus using
data-bind I can do t.red_flag too... is it possible to do that?

ceej

unread,
Aug 3, 2011, 11:50:49 AM8/3/11
to KnockoutJS
I went with https://gist.github.com/257e82a1cb54c7ecc91c which is much
better :)
Reply all
Reply to author
Forward
0 new messages