javascript - 通过 jQuery slider/Backbone 动态更新字段

标签 javascript jquery backbone.js uislider

我正在尝试使用this Backbone.js 演示根据 slider 位置动态更新字段。但是,当调用 updateVal 处理程序时,它似乎无法识别 DOM 元素的 .slider 属性。第一次使用 Backbone/js MVC 框架,所以欢迎任何/所有的智慧之言。

这是错误的屏幕截图

enter image description here

主干应用

$(document).ready(function () {
    // Initialize jquery slider
    $("#slider").slider();

    // Create the model class via Backbone (which sets up things like prototype objects correctly). 
    // This particular model is a very simple one. It'll have just 1 attribute - "slidervalue"
    var SliderModel = Backbone.Model.extend({});

    // A "View" abstraction for the slider.
    // Whenever the slider position changes, this view updates the model with the new value.
    var SliderView = Backbone.View.extend({
        el: $("#slider"), // Specifies the DOM element which this view handles

        events: {
            // Call the event handler "updateVal" when slider value changes.
            // 'slidechange' is the jquery slider widget's event type. 
            // Refer http://jqueryui.com/demos/slider/#default for information about 'slidechange'.
            "slidestart": "updateVal",
            "slidestop" : "updateVal"
        },

        updateVal: function () {
            // Get slider value and set it in model using model's 'set' method.
            console.log('SliderView.updateVal');
            var val = this.el.slider("option", "value");
            this.model.set({ slidervalue: val });
        }
    });

    // The listener "View" for the model.
    // Whenever the model's slidervalue attribute changes, this view receives the updated value.
    var ValueView = Backbone.View.extend({
        initialize: function (args) {
            // _.bindAll is provided by underscore.js. 
            // Due to some javascript quirks, event handlers which are bound receive a 'this' 
            // value which is "useless" (according to underscore's docs).
            // _.bindAll is a hack that ensures that 'this' in event handler refers to this view.
            _.bindAll(this, 'updateValue');


            console.log('ValueView.initialize');

            // Bind an event handler to receive updates from the model whenever its
            // 'slidervalue' attribute changes.
            this.model.bind('change:slidervalue', this.updateValue);
            console.log(this);
        },

        updateValue: function () {

            // Get the slider value from model, and display it in textbox.
            console.log('ValueView.updateValue');

            // this.model won't work unless "_.bindAll(this, ...)" has been called in initialize
            var value = this.model.get('slidervalue');
            console.log(value);
            $("#sliderVal").val(value);
        }
    });

    // Create the instances
    var sliderModel = new SliderModel;
    var sliderView = new SliderView({ model: sliderModel });
    var valView = new ValueView({ model: sliderModel });
});

HTML

    <!-- "slider" is a jquery slider -->
    <div id="slider"></div>

    <!-- "sliderVal" displays the slider's position. It receives the value via model. -->
    <input type="text" id="sliderVal" value="0"/>

最佳答案

尝试

 var val = this.$el.slider("option", "value");

View.el是实际的DOM对象,View.$el被包装在jquery对象中。

关于javascript - 通过 jQuery slider/Backbone 动态更新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25796804/

相关文章:

jquery - 只选择第三里

javascript - Backbone 新手: Approaching layouts

javascript - 在 CSS 中悬停时水平扩展图像

javascript - 如何让一个 div 在到达另一个 div 时滑出和滑入

javascript - 如何检查传递给 JavaScript 函数的值是否已定义或其长度是否 >=0?

javascript - KO Component - 在组件绑定(bind)中保留 DOM 元素

javascript - 获取嵌套 ul 树中的下一个标签

jquery - 如何将 html 列表分成几列

javascript - Google Maps JS v3 和 HTML5 JavaScript 框架

javascript - Backbone : adding new model to a collection is not working