capturing ko.mapping.fromjs changes

32 views
Skip to first unread message

chris green

unread,
May 20, 2015, 6:01:25 AM5/20/15
to knock...@googlegroups.com
Hi Guys,

I've been playing with knockout for a short time and could use some assistance. 

My goal is to develop an application that displays a grid of data, stock prices. These will be updated periodically from the server using Ajax.

So far I have a viewmodel that is created using ko.mapping.fromJS, this is then updating with fresh stock prices from the server using another call to ko.mapping.fromJS. All works well and my view is updated successfully. However what I want to do is capture the change on a per cell basis so that I can update the cell with green for an increase in a cell value or red for a decrease and so far I can't find a way to intercept the event - I'm toying with using expandos on the html but i've seen some negative comments on expandos and can't help but feel there must be a better way. Any ideas?

Thank you for your time.



Gunnar Liljas

unread,
May 20, 2015, 10:22:08 AM5/20/15
to knock...@googlegroups.com
I would suggest that you don't use ko.mapping at all. It tends to complicate things.

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

chris green

unread,
May 20, 2015, 2:14:25 PM5/20/15
to knock...@googlegroups.com

I see. Is there something in particular you would recommend?

You received this message because you are subscribed to a topic in the Google Groups "KnockoutJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/knockoutjs/hAyDhatgor0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to knockoutjs+...@googlegroups.com.

Gunnar Liljas

unread,
May 20, 2015, 6:06:58 PM5/20/15
to knock...@googlegroups.com
I would recommend mapping "manually" into StockPrice entities.

var StockPrice=function(val){

   
var self=this;
   
this.color=ko.observable('white');
   
var _val=ko.observable(val);
   
this.value=ko.computed({
       write
: function(newValue){
           
if (_val()==newValue)
           
{
             
self.color('white');
             
return;
           
}
           
if (_val()<newValue){
             
self.color('red');
           
} else {
             
self.color('green');
           
}
       
},
       read
: function(){
         
return _val();
       
}

   
}
  );

}









chris green

unread,
May 21, 2015, 4:21:52 AM5/21/15
to knock...@googlegroups.com

Thank you kindly. I'm going make the changes you suggest.

Reply all
Reply to author
Forward
0 new messages