1

我有一个从按钮调用的模式对话框。一切似乎都很好,除了我必须将 cancelconfirm.html 与 cancelconfirm.js 文件一起放在 viewmodels 目录中,否则它找不到 html 文件。它正在 viewmodels 目录中寻找它。

我认为这是我调用 showModal() 的方式,但我在想,既然我在路由器中有它 durandal 会知道在哪里寻找视图?

app.showModal('areas/share/viewmodels/cancelconfirm')

刚开始durandal,我错过了什么?

谢谢

这是我设置 viewLocator 和应用程序根目录的代码

    viewLocator.useConvention('../../areas/share/viewmodels', '../../areas/share/views', '../../areas/share/views');

        // show the app start view model
        app.setRoot('../../areas/share/viewmodels/shell', 'entrance');


    define(function() {
    var app = require('durandal/app');
    var logger = require('logger/logger');
    var router = require('durandal/plugins/router');


    function Share(email, downloadProxy, downloadMaster, comment, alertUser) {
        this.email = email;
        this.downloadProxy = ko.observable(downloadProxy);
        this.downloadMaster = ko.observable(downloadMaster);
        this.comment = ko.observable(comment);
        this.alertUser = ko.observable(alertUser);
    }

    var shares = ko.observableArray();  // change this variable to recipients
    var shareId = ko.observable();
    var emailValidator = $("#email").kendoValidator().data("kendoValidator");    

    var addRecipient = function() {
        emailValidator = $("#email").kendoValidator().data("kendoValidator");

        if (emailValidator.validate()) {


            logger.log("valid email address", "", "adding to share", false);
            var newShare = new Share(email.value, false, false, false, false);
            shares.push(newShare);
            emailValidator.hideMessages();                      
        }
    };

    function resetEmail() {   
        email.value = "";
        emailValidator.hideMessages();       
    }    

    var selectAll = function(share, event) {
         logger.log(share, "", "select all", false);
         if (event.currentTarget.checked == true) {
             share.downloadProxy(true);
             share.downloadMaster(true);
             share.comment(true);
             share.alertUser(true);
         } else {
             share.downloadProxy(false);
             share.downloadMaster(false);
             share.comment(false);
             share.alertUser(false);
         }

         return true;
     };

    var next = function() {
        router.navigateTo("#/share/" + shareId + "/settings");
    };

    var previous = function() {
        router.navigateTo("#/share/" + shareId + "/files");
    };

    var cancel = function() {
        app.showModal('areas/share/viewmodels/cancelconfirm').then(function(dialogResult){
            // add the storing of the user pref dontShow later
            // cookie or true preference?
            if(dialogResult.answer == 'Yes') {
                window.location.assign("asset");                                
            }
        });
    };      

    return {        
        shares : shares,
        addRecipient : addRecipient,
        selectAll : selectAll,
        resetEmail : resetEmail,        
        next : next,
        previous : previous,
        cancel : cancel,
        activate : function(context) {
            //app.showMessage("Share ==> " + shares()[0].email);
            shareId = context.id;
        }
    };
});

要求现在正在使用您建议的方法:

它仍在 viewmodels 目录中寻找视图文件。未捕获的错误:/SkyTrack/resources/areas/share/viewmodels/cancelconfirm.html HTTP 状态:404

这是我在 main.js 中用于要求配置的内容:

require.config({
    baseUrl: "/SkyTrack/resources/",
paths: {
    text: 'core/durandal/amd/text',
    durandal: "core/durandal",
    logger: "core/services",
    cancelconfirm: "areas/share/viewmodels"         
    }
});

调用模式的文件顶部的定义块:define(function() { var app = require('durandal/app'); var logger = require('logger/logger'); var router = require(' durandal/插件/路由器'); var cancelconfirm = require('cancelconfirm/cancelconfirm');

调用模态的函数

   var cancel = function() {        
        app.showModal(cancelconfirm).then(function(dialogResult){               
            // add the storing of the user pref dontShow later
            // cookie or true preference?
            if(dialogResult.answer == 'Yes') {
                window.location.assign("asset");                                
            }
        });
    };          

我还必须在我的 shell.js 顶部添加 require (我不太明白为什么,但在我这样做之前,我得到了“尚未加载上下文:_。”错误)

下面是 shell.js 的完整内容:

define(function (require) {
    var router = require('durandal/plugins/router');
    var logger = require('logger/logger');
    var cancelconfirm = require('cancelconfirm/cancelconfirm');    

    return {
        router : router,
        activate : function () {
            logger.log("shell init activate share", "", "share shell", false);

            //router.mapNav("addshare");
            router.mapNav("files");
            router.mapNav("recipients");
            router.mapNav("settings");
            router.mapNav('cancelconfirm');            

            //allows regular links to work (so you can navigate between multiple SPA's)
            Sammy.Application.prototype.disable_push_state = true;
            return router.activate('files');
        }
    };
});

** * ** * ** * **** main.js 清单

require.config({
      baseUrl: "/SkyTrack/resources/",
    paths: {
        text: 'core/durandal/amd/text',
        durandal: "core/durandal",
        logger: "core/services",
        cancelconfirm: "areas/share/viewmodels/cancelconfirm"           
    }
});


define(function (require) {
    var app = require('durandal/app'),
    viewLocator = require('durandal/viewLocator'),
    viewModelBinder = require('durandal/viewModelBinder'),
    system = require('durandal/system'),
    router = require('durandal/plugins/router'),
    logger = require('logger/logger');

    system.debug(true);

    logger.log("using durandal", "", "main", false);
    app.title = "Share My Assets";

    app.start().then(function () {

        // kendo configuration 
        // Configure kendo data binding attribute prefix
        kendo.ns = "kendo-";
        logger.log("Configured kendo data binding prefix = 'data-kendo-*'", "", "main", false);

        // Add Durandal beforeBind event handler that will apply
        // Kendo data binding every time a viewModel is bound to a view
        viewModelBinder.beforeBind = function (viewModel, view) {
        logger.log("Applying Kendo data binding", "", "viewModelBinder", false);

            // Find all Kendo templates and add to the DOM before
            // bind is called
            var templates = $("script[type='text/x-kendo-template']", view);
            templates.each(function(i,item){
                $(document.body).append(item);
            });

            try
            {
                if(viewModel.kendoViewModelPropertyName != null)
                {
                    kendo.bind(view, viewModel[viewModel.kendoViewModelPropertyName]);
                }
                else
                {
                    kendo.bind(view, viewModel);
                }
            }
            catch(e)
            {
                logger.log("Error applying kendo MVVM bindings: " + e.message);
            }

        };



        // configure routing
        router.useConvention('../../areas/share/viewmodels');
        viewLocator.useConvention('../../areas/share/viewmodels', '../../areas/share/views', '../../areas/share/views');

        app.adaptToDevice();

        // show the app start view model
        app.setRoot('../../areas/share/viewmodels/shell', 'entrance');
    });

});
4

1 回答 1

1

试试这种方法:

var cancelconfirm = require('cancelconfirm');
app.showModal(cancelconfirm);

使用这种方法,您正在加载视图模型并将其传递给showModal调用。然后showModal将使用viewLocator来定位与视图模型关联的视图。这样,您就不会过多地依赖 durandal 来找到合适的视图模型,而是允许 requirejs 为您加载模块并让 durandal 定位视图。

于 2013-06-14T14:31:48.463 回答