开发者

JavascriptMVC: does it cache models?

I'm a rank beginner with JMVC. I'm trying to figure out whether it stores models anywhere after they are retrieved from the server.

For example, the Model docs have this code snippet:

$.Controller("Tasks",
{
  init: function() {
    Task.findAll({}, this.callback('tasks'));
  },

Does calling Task.findall() save the list of tasks in a variable somewhere, like Task.tasks, or do I ne开发者_运维百科ed to store them myself?

Thanks!


No it does not seem to cache.

However, you can make your REST resource cached quite simply. Let's assume you have a RESTful resource like this.

$.Model('Example.Models.Example',
{
    findAll: REST_BASEPATH + "/example"
}

Now to make this cached, you first have re-implement that query with some explicit jQuery:

$.Model('Example.Models.Example',
{
    findAll: function(){
        $.ajax({
            url: REST_BASEPATH + "/example",
            type: 'get',
            dataType: 'json',
        })        
    }
}

Now the findAll function will return a jQuery Deferred object that JMVC is able to use. To add caching, you can store the deferred object on first call and return the same object on subsequent calls. Like this:

var cache = undefined
$.Model('Example.Models.Example',
{
    findAll: function(){
        if (!cache) {
            cache = $.ajax({
                url: REST_BASEPATH + "/example",
                type: 'get',
                dataType: 'json',
            })
        }
        return cache
    }
}

I find this somewhat kludgy, but this is what I just came up with today. If there's a more elegant way, please let me know.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜