如何在Durandal中禁用缓存视图 [英] How to disable cache view in Durandal

查看:78
本文介绍了如何在Durandal中禁用缓存视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图模型:

define(['knockout'], function (ko) {
        var self, albumWrapper;

        albumWrapper = function () {
            self = this;

            // get album name from url parameter
            var albumName = routerWrapper.router.activeInstruction().params[0];
            self.retrieveAlbum(albumName);
        };

        albumWrapper.prototype = {
            retrieveAlbum: function (name) {
                /* do something to retrieve data */
            }
        }

        return new albumWrapper();
    });

我通过以下网址配置了转到该视图的路径:#gallery / album /:name

I config route to go to this view by this url: #gallery/album/:name

但是当参数(/:name)改变时代码:

But when parameter (/:name) changed by this code:

window.location.href ='#gallery / album /'+ data.name();

此视图模型是第一次出现,因此我什么时候都无法检索到任何新专辑。我认为此问题是由于缓存视图引起的,但我不确定。
请帮助我解决此问题。在这种情况下,我使用durandal。

This viewmodel just occur first time, so I cannot retrieve any new album when. I think this problem because of cache view, but I not sure. Please help me fix this. I use durandal in this case.

推荐答案

我不认为您的问题在缓存,我认为这与您设置视图模型的方式。据我所知,durandal将第一次解析您的视图,并使用上面的代码创建视图模型。返回新的albumWrapper()将使您的视图模型成为单例。构造函数只会在第一次调用,而不会再次调用。您可以尝试在albumWrapper()之前删除new关键字,以返回函数而不是函数的新实例。这样,durandal每次都会创建一个新的视图模型,这也会导致构造函数中的代码也每次都运行。尽管更好的方法可能是使用激活功能并让模块生命周期为您服务。请参见以下更改:

I don't think your problem is caching, I think it has to do with the way you setup your view model. From what I see, durandal is going to resolve your view the first time and use your code above to create the viewmodel. Returning new albumWrapper() will cause your viewmodel to be a singleton. The constructor function is only going to be called on first time and never again. You can try removing the new keyword before albumWrapper() to return the function instead of an new instance of it. This way durandal will create a new view model each time which should cause your code in the constructor to be run each time as well. Although a better way might be to use the activate function and let the module life cycle work for you. See the changes below:

define(['knockout'], function (ko) {
    var self, albumWrapper;

    albumWrapper = function () {
        var self = this;

        self.CurrentAlbum = ko.observable();
    };

    albumWrapper.prototype = {
        retrieveAlbum: function (name) {
            /* do something to retrieve data */
        },
        activate: function(name) {
            var self = this;    
            var selectedAlbum = self.retrieveAlbum(name);
            self.CurrentAlbum(selectedAlbum);
        }
    }

    return albumWrapper();
});

现在durandal应该可以获取一个视图实例,但是每次请求路线。还添加了一个称为当前相册的可观察字段,您可以将其绑定到视图元素。离开路线不会导致视图实例像网页一样从内存中删除,它只会停留在那儿,直到下次您再次导航到路线时,更像是桌面应用程序将屏幕换成了屏幕。每次导航都会调用激活功能,您可以在该视图中设置路线上新专辑名称的视图。假设retrieveAlbum函数返回具有名称,日期,艺术家等属性的唱片集对象,则可以将其设置为可观察到的当前唱片集的值,这将自动更新视图中的元素。在此处查看有关路由和视图生命周期的文档:

Now durandal should get one instance of your view but call the activate function with any route parameters each time the route is requested. Also added is an observable field called Current Album that you can bind your view elements to. Navigating away from the route does not cause the instance of the view to be removed from memory like a web page, it just sits there till the next time you navigate to the route again, more like a desktop app trading out screens. The activate function is called on every navigation and lets you setup the view for new album name on the route. Assuming the retrieveAlbum function returns an album object with properties like name, date, artist, etc, you can set that as the value of Current Album observable which will automatigically update the elements on your view. Check out the docs on routing and the view life cycle here:

http://durandaljs.com/documentation/Using-The-Router.html

http://durandaljs.com/documentation/Hooking-Lifecycle-Callbacks.html

这篇关于如何在Durandal中禁用缓存视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆