在knockout绑定中使用dropzone.js

时间:2015-07-07 14:42:47

标签: knockout.js dropzone.js

我试图让dropzone作为淘汰赛绑定工作。我真的希望能够......

ko.bindingHandlers.dropzone = {
    init: function(element, valueAccessor, allBindingsAccessor, bindingContext) {
        $(element).dropzone({ url: 'some/tightly/bound/uri});
    }
}

..但我不能。根据在viewmodel中输入的数据,uri是动态的,所以这是我到目前为止所提出的:

var dropzoneObject; // probably should be this.dropzoneObject

ko.bindingHandlers.dropzone = {
    init: function(element, valueAccessor, allBindingsAccessor, bindingContext) {
        var url = allBindingsAccessor().urlPath || "unknown";
        dropzoneObject = new Dropzone("div#" + element.id, {
            url: url, 
            init: ...,
            etc
        });
    },
    update: function(element, valueAccessor, allBindingsAccessor, bindingContext) {
        var url = allBindingsAccessor().urlPath || "unknown";
        dropzoneObject.options = {
            url: url
        };
    }
}

但是当我测试这个时,我收到以下错误:

Uncaught TypeError: Cannot read property 'trim' of undefinedDropzone.defaultOptions.addedfile 
@ dropzone.js:252Emitter.emit @ dropzone.js:58Dropzone.addFile 
@ dropzone.js:956(anonymous function) @ dropzone.js:563

为什么undefinedDropzone?我错过了什么?

由于

1 个答案:

答案 0 :(得分:5)

找到答案,我的更新方法是编写所有选项(包括默认选项),而不是更新它:

....
update: function(element, valueAccessor, allBindingsAccessor, bindingContext) {
    var url = allBindingsAccessor().urlPath || "unknown";
    dropzoneObject.options = {
        url: url
    };
}
....

应阅读:

update: function(element, valueAccessor, allBindingsAccessor, bindingContext) {
    var url = allBindingsAccessor().urlPath || "unknown";
    dropzoneObject.options.url = url;
}

希望这有助于某人!