Search code examples
jqueryjsongetjsondurandal

Durandal and jQuery getJSON: should I getJSON in each module's activate?


I'm learning some Durandal and have an SPA working -- I call jQuery's getJSON JSON data set within each "page's" activate method, and then burrow down in that data to get the data for that specific page.

I was thinking: this means I'm retrieving the same data each time a page is first activated. Is this 'best practice'? Is it more reasonable to only load the data once? But how does this work?

Is there a way using jQuery to mimic SQL calls and only retrieve that portion of the JSON data that's relevant to my page? (I have separate objects within the JSON for each page)

Thanks for any insight!


Solution

  • I was thinking: this means I'm retrieving the same data each time a page is first activated. Is this 'best practice'? Is it more reasonable to only load the data once? But how does this work?

    This depends on the the complexity of the application. Keep in mind that one of the key principles of require.js is modularity. Using a single 'root' datasource within all your modules would violate that principle by making maintainability difficult in large applications, hence making it bad practice.

    Is it more reasonable to only load the data once?

    The reasonability lies with the developer of the application and the circumstance the developer is in. If you are working on a team, having a single 'root' datasource will making things difficult to maintain in the sense that you have to make sure that 'root' data source is always in sync with the most up to date data and all the modules receive the most updated data.

    Is there a way using jQuery to mimic SQL calls and only retrieve that portion of the JSON data that's relevant to my page? (I have separate objects within the JSON for each page)

    You have prep your server side to work with your client side. Try implementing some sort of an internal rest API to help you partition to its relevant module.