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?

3
  • 3
    If 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. Commented Aug 16, 2012 at 6:27
  • Are you willing to use a javascript framewrok which is already avaialable ? Commented 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. Commented Aug 16, 2012 at 14:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.