javascript - 将DIV元素属性的绑定(bind)拖动到knockoutjs中的对象属性

标签 javascript jquery html css knockout.js

我这里有一个代表 div 元素的对象

function ElementVM
{
    var self = this;

    self.Height = ko.observable();
    self.Width = ko.observable();
    self.Bottom = ko.observable();
}

并将其绑定(bind)到

<div data-bind="style:{ height: Height, width: Width, bottom: Bottom, position: 'absolute' }">

但问题是,一旦这个 div 元素被拖动,因为我已将其设置为可拖动,它不会更改我的 ElementVM 对象的值。

您是否知道绑定(bind)应如何为此工作?

谢谢。

最佳答案

为此,您需要使用自定义绑定(bind)。您可以在下面或 this jsfiddle 中看到它.

// http://knockoutjs.com/documentation/custom-bindings.html
ko.bindingHandlers.draggable = {
     init : function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called when the binding is first applied to an element
        // Set up any initial state, event handlers, etc. here
        var $element = $(element);
        var position = ko.unwrap(valueAccessor());
        if (position) {
            if (position.left) 
                $element.css({left:ko.unwrap(position.left)});
            if (position.top) 
                $element.css({top:ko.unwrap(position.top)});
            if (position.height) 
                $element.height(ko.unwrap(position.height));
            if (position.width) 
                $element.width(ko.unwrap(position.width));
        }
        var draggable = $element.draggable({
            stop: function(event,ui) {
                if (position) {
                    if (ko.isWritableObservable(position.left))
                       position.left(ui.position.left);
                    if (ko.isWritableObservable(position.top))
                       position.top(ui.position.top);
                    if (ko.isWritableObservable(position.height))
                       position.height($element.height());
                    if (ko.isWritableObservable(position.width))
                       position.width($element.width());
                }
            }
        });
    },
    update : function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called once when the binding is first applied to an element,
        // and again whenever any observables/computeds that are accessed change
        // Update the DOM element based on the supplied values here.
        var position = ko.unwrap(valueAccessor());
        var $element = $(element);
        if (position.left)
			$element.css({left:ko.unwrap(position.left)+'px'});
        if (position.top)
			$element.css({top:ko.unwrap(position.top)+'px'});
        if (position.height)
            $element.height(ko.unwrap(position.height));
        if (position.width)
            $element.width(ko.unwrap(position.width));
    }
};

var vm = {
    position: ko.observable({
    	left: ko.observable(150),
    	top: ko.observable(125),
    	width: ko.observable(100),
    	height: ko.observable(40)
    })
};

ko.applyBindings(vm);
div {
    border: solid 1px #444;
    background-color: #DDD;
    text-align: center;
    padding: 12px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>  
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>

<div data-bind="draggable: position"
     style="position:absolute;">
    This is a draggable div
</div>
<ul>
    <li>Top: <input type="textbox" data-bind="value: position().top"/></li>
    <li>Left: <input type="textbox" data-bind="value: position().left"/></li>
    <li>Width: <input type="textbox" data-bind="value: position().width"/></li>
    <li>Height: <input type="textbox" data-bind="value: position().height"/></li>
</ul>
<pre data-bind="text:JSON.stringify(ko.toJS($root),null,2)"></pre>

关于javascript - 将DIV元素属性的绑定(bind)拖动到knockoutjs中的对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33294683/

相关文章:

javascript - jQuery 仅当 div 不为空时才追加

jQuery 如何将类添加到特定的 div

javascript - 在 HTML 中为 ng-animation 强制行高

javascript - 在 Javascript 中动态添加新表行

javascript - 替换带引号的字符串中的所有逗号

javascript - IndexedDB 打开数据库请求奇怪的行为

javascript - 灯箱的使用和点击事件

Canvas 和 Javascript 对象上的 Javascript 动画

JavaScript 扩展方法

javascript - 我如何在 TypeScript 中定位 ES2018?