【问题标题】:How to use TinyMCE in Durandal (Knockout, RequireJS)如何在 Durandal 中使用 TinyMCE (Knockout, RequireJS)
【发布时间】:2015-11-09 19:36:03
【问题描述】:

我从几个小时开始就尝试让 TinyMCE 在我的 SPA 中运行。我已经找到了一些答案,但对我不起作用。

例如这个 JSFiddle http://jsfiddle.net/scottmessinger/yJ5GV/2/;

他们更多的是关于如何绑定数据,而不是如何启动 TinyMCE^^ 如果我将脚本放在 index.html 中并在 index.html 中调用它 “textarea”是查看结果的唯一方法。

Main.js(加载 tinymce 脚本)

requirejs.config({
 paths: {
    'text': '../lib/require/text',
    'durandal':'../lib/durandal/js',
    'plugins' : '../lib/durandal/js/plugins',
    'transitions' : '../lib/durandal/js/transitions',
    'knockout': '../lib/knockout/knockout-3.1.0',
    'jquery': '../lib/jquery/jquery-1.9.1',
    'bootstrap': '../lib/bootstrap/js/bootstrap',
    'lodash': '../lib/lodash/lodash',
    'jqueryUI': '../lib/jquery/jquery-ui.min',
    'tinymce': '../lib/tinymce/tinymce.min',
    'tinymceJquery': '../lib/tinymce/jquery.tinymce.min'
},
shim: {
    'jquery': {
        exports: '$'
    },
    'bootstrap': {
        deps: ['jquery'],
        exports: 'jQuery'
   }
}
});

Verwaltung.js

define([
'services/accountrepository',
'plugins/router',
'services/blogrepository',
'durandal/app',
], function (accountrepository, router, blogrepository, app) {

ko.bindingHandlers.tinymce = {
    init: function (element, valueAccessor, allBindingsAccessor, 
                    context, arg1, arg2) {
        var options = allBindingsAccessor().tinymceOptions || {};
        var modelValue = valueAccessor();
        var value = ko.utils.unwrapObservable(valueAccessor());

        var el = $(element);
        var id = el.attr('id');

        //options.theme = "advanced";
        options.theme = "modern";

        options.menubar = false;
        options.plugins = [
            "advlist autolink autosave link image lists charmap print preview hr anchor pagebreak spellchecker",
            "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
            "table contextmenu directionality template textcolor paste fullpage textcolor"
        ];
        //tinymce buttons
        //http://www.tinymce.com/tryit/classic.php
        options.toolbar_items_size = 'small';
        options.toolbar1 =
        "bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | forecolor backcolor | hr removeformat | subscript superscript  ";

        ////handle edits made in the editor. Updates after an undo point is reached.
        options.setup = function (editor) {
            editor.on('change', function (e) {
                if (ko.isWriteableObservable(modelValue)) {
                    var content = editor.getContent({ format: 'raw' });
                    modelValue(content);
                }
            });

        };

        el.html(value);

        $(element).tinymce(options);

        //handle disposal (if KO removes by the template binding)
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            var tinymceInstance = tinyMCE.get(id);
            if (tinymceInstance != undefined) {
                // if instance already exist, then get rid of it... we don't want it
                tinymceInstance.remove();
            }
        });              
    },
    update: function (element, valueAccessor, allBindingsAccessor, 
                      context) {

        var el = $(element);
        var value = ko.utils.unwrapObservable(valueAccessor());
        var id = el.attr('id');

        //handle programmatic updates to the observable
        // also makes sure it doesn't update it if it's the same. 
        // otherwise, it will reload the instance, causing the cursor to jump.
        if (id !== undefined) {
            //$(el).tinymce();

            var tinymceInstance = tinyMCE.get(id);
            if (!tinymceInstance)
                return;
            var content = tinymceInstance.getContent({ format: 'raw' });
            if (content !== value) {
                //tinymceInstance.setContent(value);
                valueAccessor(content);
                //$(el).html(value);
            }
        }
    }

};

var viewmodel = function () {
     var self = this;

    self.fieldValue = ko.observable("two");

 };
return viewmodel;
});

Verwaltung.html

<textarea data-bind="tinymce: FieldValue"></textarea>      

【问题讨论】:

    标签: javascript jquery knockout.js tinymce durandal


    【解决方案1】:

    理论上这应该可以工作,因为 Knockout 仅在 DOM 已加载并且元素准备好后才构成此视图,但是 Durandal 可以对此稍加挑选。 Durandal 确实有一些事件可以用来加载这个插件。 就我个人而言,我会将所有这些代码从 Knockout 扩展中取出,并将其放在 Durandal ViewModel 或 Widget 中。 使用一个模型,您可以连接到 compositionComplete 事件中循环事件。 如果您希望组合成多个视图,则可以使用 Durandal 小部件。

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

    我认为在尝试加载插件之前,您可能需要掌握创建视图/视图模型、小部件和组合的方法。只是为了让您了解框架的这些核心领域。 Durandal 是一个很棒的工具,所以请坚持下去!

    【讨论】:

    • 嘿加里,感谢您的回复。我成功安装了 durandal 的小部件功能,但它仍然无法工作.. 我将 viewmodel.js 中提到的代码和控制台中的视图 (
    • 好吧,算了。它现在作为小部件运行得很好,我只是想念加载 jquery.tinymce.js ..... 有时.. 你知道吗? :D
    猜你喜欢
    • 2013-06-25
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 2016-12-28
    • 2014-05-12
    • 2013-04-30
    • 2015-01-20
    • 1970-01-01
    相关资源
    最近更新 更多