0

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?

coding_hero
  • 1,701
  • 3
  • 19
  • 33
  • 3
    If the JSON is in a variable check here: http://stackoverflow.com/questions/1759987/listening-for-variable-changes-in-javascript-or-jquery or: http://stackoverflow.com/questions/3267741/jquery-trigger-on-variable-change these are for triggering functino on a variable change. Make the function refresh the table. – Adam Merrifield Aug 16 '12 at 06:27
  • Are you willing to use a javascript framewrok which is already avaialable ? – Diode Aug 16 '12 at 08: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 Aug 16 '12 at 14:21

2 Answers2

3

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).

VDP
  • 6,128
  • 4
  • 27
  • 52
  • Thanks for the input. I'll investigate Knockout. I'm trying to make a jQuery plugin, so I wanted to keep it pure jQuery, but I'll see what I can do with the links that you guys gave me. – coding_hero Aug 16 '12 at 15:49
2

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.

Dane Balia
  • 4,259
  • 5
  • 26
  • 48