Backbone.js移动滑块 - 通过视图函数传递变量

时间:2013-04-04 03:17:15

标签: javascript jquery backbone.js mobile-website

我正在按照本教程构建移动滑块:http://mobile.smashingmagazine.com/2012/06/21/play-with-hardware-accelerated-css/

唯一的区别是,我正在尝试将其合并到Backbone中。但是,我遇到了一个变量问题。任何人都可以就如何使其发挥作用提出一些建议。在View的函数中传递这些变量是问题所在:

var sliding = startClientX = startPixelOffset = pixelOffset = currentSlide = 0,
slideCount = $('.slide').length;

完整主干视图:

WhiteDeals.Views.EditorView = Backbone.View.extend({

initialize: function() {
    _.bindAll(this, 'render', 'slideStart', 'slide');

    var sliding = startClientX = startPixelOffset = pixelOffset = currentSlide = 0,
    slideCount = $('#dealSlide').length;

},

events: {
    "touchstart"             : "slideStart",
    "mousedown"              : "slideStart",
    "mouseup"                : "slideEnd",
    "touchend"               : "slideEnd",
    "mousemove"              : "slide",
    "touchmove"              : "slide"
},

slideStart: function(event) {
    if (event.originalEvent.touches)
      event = event.originalEvent.touches[0];
    if (sliding == 0) {
      sliding = 1;
      startClientX = event.clientX;
    }
}, // End slideStart

slide: function(event) {
    event.preventDefault();
    if (event.originalEvent.touches)
        event = event.originalEvent.touches[0];
        var deltaSlide = event.clientX - startClientX;

    if (sliding == 1 && deltaSlide != 0) {
      sliding = 2;
      startPixelOffset = pixelOffset;
    }

    if (sliding == 2) {
      var touchPixelRatio = 1;
      if ((currentSlide == 0 && event.clientX > startClientX) ||
          (currentSlide == slideCount - 1 && event.clientX < startClientX))
        touchPixelRatio = 3;
      pixelOffset = startPixelOffset + deltaSlide / touchPixelRatio;
      $('#dealSlider').css('transform', 'translate3d(' + pixelOffset + 'px,0,0)').removeClass();
    }
},

slideEnd: function(event) {
    if (sliding == 2) {
        sliding = 0;
        currentSlide = pixelOffset < startPixelOffset ? currentSlide + 1 : currentSlide - 1;
        currentSlide = Math.min(Math.max(currentSlide, 0), slideCount - 1);
        pixelOffset = currentSlide * -$('body').width();
        $('#temp').remove();
        $('<style id="temp">#dealSlider.animate{transform:translate3d(' + pixelOffset + 'px,0,0)}</style>').appendTo('head');
        $('#dealSlider').addClass('animate').css('transform', '');
    }
}, // End slideEnd

render: function() {
    this.$el.html(JST['editor/view']());
    return this;
}

}); // End of Views EditorView

1 个答案:

答案 0 :(得分:1)

我会将这些变量打包到模型中,例如:

var CoordsModel = Backbone.Model.extend({})
var coord = new CoordsModel();

然后将其传递给WhiteDeals视图:

var view = new WhiteDeals({model: coord});

然后通过模型&#39;来实现所有功能。 getters / setter我可以访问它的变量,例如:

initialize: function() {
    _.bindAll(this, 'render', 'slideStart', 'slide');

    // var sliding = startClientX = startPixelOffset = pixelOffset = currentSlide = 0

    this.model.set('sliding', 0);
    this.model.set('startClientX', 0);
    ...
    this.model.set('currentSlide', 0);

    slideCount = $('#dealSlide').length;

},
...
slideStart: function(event) {
    if (event.originalEvent.touches)
        event = event.originalEvent.touches[0];
    if (sliding == 0) {
        // sliding = 1;
        // startClientX = event.clientX;
        this.model.set('sliding', 1);
        this.model.set('startClientX', event.clientX);
    }
}, // End slideStart