【问题标题】:Aurelia with knockout secure bindingAurelia 带敲除式安全绑定
【发布时间】:2016-10-28 08:18:07
【问题描述】:

您好,我有一个使用 Aurelia CLI 运行的 Aurelia Web 应用程序。

之前,我使用 SystemJS 作为模块加载器,但由于我想强制执行我的应用程序的 Content-Security-Policy 以不允许“不安全评估”,因此我按照 @ 中的建议更改为 Aurelia CLI 987654322@.

我的应用是以前的 Durandal 应用,我将其转换为 Aurelia,因此它广泛使用了 Knockout。我正在使用 aurelia-knockout 插件(您可以在我的 main.js 中看到)

export function configure(aurelia) {
    aurelia.use
        .standardConfiguration()
        .developmentLogging()        
        .plugin('aurelia-knockout');

    return aurelia.start().then(() => aurelia.setRoot());
}

我已经安装了 knockout 和 knockout-secure-binding npm 包,并在 aurelia.json 文件中配置了它们:

"dependencies": [
          "aurelia-binding",
          "aurelia-bootstrapper",
          "aurelia-dependency-injection",
          "aurelia-event-aggregator",
          "aurelia-framework",
          "aurelia-history",
          "aurelia-history-browser",
          "aurelia-loader",
          "aurelia-loader-default",
          "aurelia-logging",
          "aurelia-logging-console",
          "aurelia-metadata",
          "aurelia-pal",
          "aurelia-pal-browser",
          "aurelia-path",
          "aurelia-polyfills",
          "aurelia-route-recognizer",
          "aurelia-router",
          "aurelia-task-queue",
          "aurelia-templating",
          "aurelia-templating-binding",
          {
            "name": "text",
            "path": "../scripts/lib/text"
          },
          {
            "name": "aurelia-templating-resources",
            "path": "../node_modules/aurelia-templating-resources/dist/amd",
            "main": "aurelia-templating-resources"
          },
          {
            "name": "aurelia-templating-router",
            "path": "../node_modules/aurelia-templating-router/dist/amd",
            "main": "aurelia-templating-router"
          },
          {
            "name": "aurelia-knockout",
            "path": "../node_modules/aurelia-knockout/dist/amd",
            "main": "aurelia-knockout"
          },
          {
            "name": "knockout",
            "path": "../node_modules/knockout/build/output",
            "main": "knockout-latest"
          },
          {
            "name": "knockout-secure-binding",
            "path": "../node_modules/knockout-secure-binding/dist",
            "main": "knockout-secure-binding.min"
          },
          {
            "name": "aurelia-testing",
            "path": "../node_modules/aurelia-testing/dist/amd",
            "main": "aurelia-testing",
            "env": "dev"
          }
        ]

我知道我必须使用以下代码(就像我在旧的 Durandal 应用中所做的那样)来激活淘汰赛安全绑定:

var options = {
        attribute: "data-bind",        // default "data-sbind"
        globals: window,               // default {}
        bindings: ko.bindingHandlers,  // default ko.bindingHandlers
        noVirtualElements: false       // default true
    };
ko.bindingProvider.instance = new ko.secureBindingsProvider(options);

但我不知道如何在这个新的 Aurelia 应用程序中使用它,或者一般来说如何使 aurelia-knockout 插件使用安全绑定版本的敲除。 我试过像这样修改我的 main.js 文件:

export function configure(aurelia) {        
    var options = {
        attribute: "data-bind",        // default "data-sbind"
        globals: window,               // default {}
        bindings: ko.bindingHandlers,  // default ko.bindingHandlers
        noVirtualElements: false       // default true
    };
    ko.bindingProvider.instance = new ko.secureBindingsProvider(options);

    aurelia.use
        .standardConfiguration()
        .developmentLogging()        
        .plugin('aurelia-knockout');

    return aurelia.start().then(() => aurelia.setRoot());
}

甚至调整 npm 模块中的 aurelia-knockout.js 文件,在此行之前插入安全绑定定义

ko.applyBindings(executionContext, this.element);

但是,即使我没有遇到任何构建错误(我使用“au build”命令构建)以上都不起作用。

更新

我直接在里面插入了ko安全绑定初始化代码

node_modules\aurelia-knockout\dist\amd\aurelia-knockout-custom-attribute.js

我在第一行添加了对安全绑定模块的引用

define(['exports', 'aurelia-dependency-injection', 'aurelia-templating', 'knockout-secure-binding'], function (exports, _aureliaDependencyInjection, _aureliaTemplating, _knockoutSecureBinding) {

现在我收到此错误:

Unhandled rejection TypeError: ko.secureBindingsProvider is not a constructor

好像没有找到/加载安全绑定模块。

无论如何,我不认为使用 node_module 的“内置”版本是理想的解决方案,我只是在寻找一种使其工作的方法。仍然期待更好的建议。

【问题讨论】:

    标签: knockout.js aurelia


    【解决方案1】:

    好的,我找到了一个可行的解决方案:

    首先,在 aurelia.json 文件中,我这样定义“loader”属性:

    "loader": {
          "type": "require",
          "configTarget": "vendor-bundle.js",
          "includeBundleMetadataInConfig": "auto",
          "config": {
            "waitSeconds": 0,
            "paths": {
              "jquery": "../scripts/lib/cdn/jquery-3.1.0.min",
              "knockout": "../scripts/lib/knockout-3.4.0"          
            }
          },
          "plugins": [
            {
              "name": "text",
              "extensions": [
                ".html",
                ".css"
              ],
              "stub": true
            }
          ]
        },
    

    这样我已经“全局”定义了淘汰赛。 “bundles”属性是这样定义的:

    "bundles": [
          {
            "name": "app-bundle.js",        
            "source": [
              "[**/*.js]",
              "**/*.{css,html}"
            ]
          },
          {
            "name": "vendor-bundle.js",
            "prepend": [
              "node_modules/bluebird/js/browser/bluebird.core.js",          
              "scripts/lib/require.js"
            ],
            "dependencies": [
              "aurelia-binding",
              "aurelia-bootstrapper",
              "aurelia-dependency-injection",
              "aurelia-event-aggregator",
              "aurelia-fetch-client",
              "aurelia-framework",
              "aurelia-history",
              "aurelia-history-browser",
              "aurelia-loader",
              "aurelia-loader-default",
              "aurelia-logging",
              "aurelia-logging-console",
              "aurelia-metadata",
              "aurelia-pal",
              "aurelia-pal-browser",
              "aurelia-path",
              "aurelia-polyfills",
              "aurelia-route-recognizer",
              "aurelia-router",
              "aurelia-task-queue",
              "aurelia-templating",
              "aurelia-templating-binding",
              {
                "name": "text",
                "path": "../scripts/lib/text"
              },
              {
                "name": "aurelia-templating-resources",
                "path": "../node_modules/aurelia-templating-resources/dist/amd",
                "main": "aurelia-templating-resources"
              },
              {
                "name": "aurelia-templating-router",
                "path": "../node_modules/aurelia-templating-router/dist/amd",
                "main": "aurelia-templating-router"
              },
              {
                "name": "aurelia-knockout",
                "path": "../node_modules/aurelia-knockout/dist/amd",
                "main": "aurelia-knockout"
              },          
              {
                "name": "aurelia-testing",
                "path": "../node_modules/aurelia-testing/dist/amd",
                "main": "aurelia-testing",
                "env": "dev"
              },
              {
                "name": "icheck",
                "path": "../node_modules/icheck",
                "main": "icheck.min"
              },
              {
                "name": "filesaver.js",
                "path": "../node_modules/filesaver.js",
                "main": "FileSaver.min"
              }
            ]
          }
    

    现在我在应用程序启动之前在 main.js 中加载我需要的所有插件,并将相应的插件添加到 globak "ko" 变量中:

        export function configure(aurelia) {    
            aurelia.use
                .standardConfiguration()        
                .globalResources('views/panel_header.html')        
                .plugin('aurelia-knockout');
    
            aurelia.use.developmentLogging();
            var prefix = '../';
            var scripts = [
                prefix + "/scripts/lib/breeze.debug",
                prefix + "/scripts/lib/knockout-secure-binding",
                prefix + "/scripts/lib/knockout.binding.handlers",
                prefix + "/scripts/lib/knockout.wrap",
                prefix + "/scripts/lib/knockout.dirtyFlag",
                prefix + "/scripts/lib/knockout-sortable.min"
            ];
    
            return aurelia.start().then(() => {
                require(scripts, function(breeze, secureBindingProvider, kbh, wrap, dirtyFlag, sortable) {
                    require([
                        prefix + "/scripts/lib/breeze.savequeuing",
                    ], function(savequeuing){
                        ko.bindingProvider.instance = new ko.secureBindingsProvider({
        attribute: "data-bind",        // default "data-sbind"
        globals: window,               // default {}
        bindings: ko.bindingHandlers,  // default ko.bindingHandlers
        noVirtualElements: false       // default true
    });
                        ko.wrap = wrap;            
                        ko.dirtyFlag = dirtyFlag;
                        ko.sortable = sortable;
                        aurelia.setRoot();
                    });             
                });                     
            });    
        }
    

    现在,当 aurelia-knockout 插件执行绑定时,它会按照我的意愿使用“knockout-secure-binding”实例。

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 1970-01-01
      • 2014-01-03
      • 2014-06-27
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      相关资源
      最近更新 更多