Ember:如何将TinyMCE textarea字段绑定到模型

cyc*_*arc 5 javascript tinymce ember.js

我在模板中嵌入了TinyMCE.现在,我想重新绑定TinyMCE编辑器的内容(实际上是一个textarea).

http://jsfiddle.net/cyclomarc/wtktK/10/

在文本字段中输入文本时,{{bodyText}}中的文本会更新.我还想更新TinyMCE textarea中的文本......

知道怎么做吗?

HTML:

<script type="text/x-handlebars">
    <h2>Tiny MCE</h2>
    {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="index">
        <form method="post" action="somepage">
            App.IndexController.bodyText value:</br>
            {{bodyText}}
            </br></br>

            Bound to Ember.TextField:<br>
            {{view Ember.TextField valueBinding='bodyText'}}

            </br></br>
            Bound to Ember.TextArea:</br>
            {{view Ember.TextArea valueBinding='bodyText'}}

        </form>
</script>
Run Code Online (Sandbox Code Playgroud)

JS:

var App = Ember.Application.create({
  LOG_TRANSITIONS: true
});

App.Router.map(function () {});

App.IndexRoute = Ember.Route.extend({ });

App.IndexController = Ember.Controller.extend({
    bodyText: '...' 
});

App.IndexView = Ember.View.extend({
    didInsertElement: function(){
        tinymce.init({
            selector: "textarea"
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

Mar*_*ior 12

我在你的小提琴里做了一些改变.

看看这里http://jsfiddle.net/Jw75L/26/

首先,我删除了var来自App声明,成为App全局命名空间,并在Handlebars模板中可见.

并将tinymce替换IndexViewTinymceView,可重复使用.

App.TinymceView = Ember.TextArea.extend({
    editor: null,
    _suspendValueChange: false,
    didInsertElement: function(){
        var id = "#" + this.get("elementId");        
        var view = this;
        tinymce.init({
            selector: id,
            setup : function(ed) {                
                view.set("editor", ed);
                ed.on("keyup change", function() {
                    view.suspendValueChange(function() {
                        view.set("value", ed.getContent());
                    });
                });
           }
        });
    },
    suspendValueChange: function(cb) {
        this._suspendValueChange = true;
        cb();
        this._suspendValueChange = false;
    },
    valueChanged: function() {
        if (this._suspendValueChange) { return; }
        var content = this.get("value");
        this.get("editor").setContent(content);
    }.observes("value"),
    willClearRender: function() {        
        this.get("editor").remove();
    }
});
Run Code Online (Sandbox Code Playgroud)

由于TinyMCE的改变textarea的,创造了很多元素的,我有这样的观察员在TinyMCE的变化,并传播到TinymceView,using ditor.on("keyup"....