I know enough jQuery/JavaScript to be dangerous. I have a JSON array that I'm interacting with using two different elements (a calendar and a table, to be precise). Is there an event handler (or any other way) I could bind to so that the table would refresh when the JSON changes?
-
3If the JSON is in a variable check here: stackoverflow.com/questions/1759987/… or: stackoverflow.com/questions/3267741/… these are for triggering functino on a variable change. Make the function refresh the table.Adam Merrifield– Adam Merrifield2012-08-16 06:27:43 +00:00Commented Aug 16, 2012 at 6:27
-
Are you willing to use a javascript framewrok which is already avaialable ?Diode– Diode2012-08-16 08:53:20 +00:00Commented Aug 16, 2012 at 8:53
-
Thanks for the links, @AdamMerrifield! I think I can find something in there. I was trying to write my first jQuery plugin, so I'm not sure if using Knockout or another framework would work if I want it all to be self-contained.coding_hero– coding_hero2012-08-16 14:21:38 +00:00Commented Aug 16, 2012 at 14:21
2 Answers
Basic programming, parse the json (=string) into a javascript object or array. (you probably have already done that.) Use an implementation of the observer patern.
I suggest taking a good look at @Adam Merrifield 's interesting links.
Most of the time using getters and setter where you can fire a custom event (or call a callback method) inside a setter is the key in this.
KnockoutJS is a good framework to help you do such binding. It also uses the observable - observer/subscriber pattern.
using timers is not a really good idea.. little to much overhead. (doing stuff also when nothing gets changed. And you will always hop x ms behind (depending on the polling frequency).
1 Comment
You might want to consider Knockout.JS It allows bi-directional mapping, so a change to your model should reflect on your view and vice/versa.
http://knockoutjs.com/documentation/json-data.html
However, it might be late stages of your dev cycle, but something to consider.