0

ページの読み込み時に自動サイズ設定したいjquery autosize用のカスタムノックアウトバインディングがあります。

これが問題のフィドルです

コードの例:

 ko.bindingHandlers.autogrow = {

 init: function (element, valueAccessor, allBindingsAccessor) {

     ko.utils.registerEventHandler(element, 'focusout', function () {
         var observable = valueAccessor();
         observable($(element).val());
     });

     ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
         $(element).data('autosize').remove();
     });

     $(element).autosize({ append: "\n" });

     $(element).focus(function () {
         $(element).trigger('autosize');
     });
 },

 update: function (element, valueAccessor) {
     var value = ko.utils.unwrapObservable(valueAccessor());
     $(element).val(value);
     $(element).trigger("autosize");
   }
};

var vm = {};
vm.Data = ko.observable("This is a lot of text and has to display correctly");

ko.applyBindings(vm);

html の例:

<textarea id="autogrow" class="text-nm span2" data-bind="autogrow: Data"></textarea>

何が起こるかというと、テキスト領域に表示したいテキストがたくさんありますが、その量は毎回異なるため、事前に知っている表示量が設定されていません。私がしたいのは、ノックアウト バインディングが適用された後です。オートサイズ プラグインのオートサイズ イベントをトリガーしたいのですが、いつ、どこで行うかわかりません。

ありがとう

4

1 に答える 1

2

I've might misunderstood the question, but this works in FF atleast

http://jsfiddle.net/3p9bj/13/

I removed the update function and let autosize do its thing under the hood

I also use the value binding for updating value

ko.applyBindingsToNode(element, { value: valueAccessor() }); 
于 2013-02-25T09:05:57.810 に答える