javascript - 主干 - 尝试使用集合中的下一个或上一个模型重新渲染 View

标签 javascript backbone.js

我正在尝试进入 Backbone 并进行以下尝试,这是尝试做一个图像库。我正在尝试将渲染与集合中的模型一起使用。我将显示集合的第一个元素,但我想添加对简单地重新渲染下一个元素的支持,但我不知道如何执行此操作。

我已经在我的模型上实现了下一个和上一个,如下所示:

arc.Item = Backbone.Model.extend({
    next: function () {
        if (this.collection) {
            return this.collection.at(this.collection.indexOf(this) + 1);
        }
    },previous: function () {
        if (this.collection) {
            return this.collection.at(this.collection.indexOf(this) - 1);
        }
    }
});

这里的问题(可能不止我要问的问题)出在 loadNext 方法中。我如何获取此集合中的当前位置并增加它?

arc.ItemsGalleryView = Backbone.View.extend({
      el: $('#mig-container'),
    events: {'click .next-btn' : 'loadNext', 
             'click .previous-btn':'loadPrevious' },
       template:_.template($('#mig-image-tmp').text()),
      initialize: function() {
          _.bindAll( this, 'render' );
          // render the initial state
          var thisModel=this.collection.first();
          this.render(thisModel);
      },

      render: function(xModel) {  // <- is it ok to do it this way?
          var compiled=this.template(xModel.toJSON());
          this.$el.html(compiled);
          return this;
      },

      loadNext: function(){
        console.log('i want to load Next');
        this.render(this.collection.next());  // <- how would I do this
        this.render(this.collection.first().next());  // <- this works by always giving me the second.
        // I need to replace first with current

      },
      loadPrevious: function(){
        console.log('i want to load Previous');
      }

或者有更好的方法来实现这个吗?

提前致谢

编辑 #1

 arc.ItemsGalleryView = Backbone.View.extend({
        el: $('#mig-container'),
        events: {'click .next-btn' : 'loadNext', 'click .previous-btn':'loadPrevious' },
         template:_.template($('#mig-image-tmp').text()),
        initialize: function() {
          _.bindAll( this, 'render' );
          this.render(this.collection.first());  // <- this works correct
        },

        render: function(xModel) {
          console.log(xModel.toJSON());
          var compiled=this.template(xModel.toJSON());
          this.$el.html(compiled);
          return this;
        },
        loadNext: function(){
          console.log('i want to load next');
          this.render(this.collection.next());   // <- this doesn't seem to do anything, event is called correctly but doesn't seem to move to next element
        },

    However if I adjust to this, it will load the 3rd element of the array

loadNext: function(){
  console.log('i want to load Previous');
  this.render(this.collection.at(2));
},

我如何使用 this.collection.next() 来获得这种行为?

谢谢

最佳答案

您正在寻找的是一种使用Collection来操作下一个/上一个内容的方法。您当前拥有的只是将其放在模型上。这是我在项目中使用的基本Collection:

App.Collection = Backbone.Collection.extend({
  constructor: function(models, options) {
    var self = this;
    var oldInitialize = this.initialize;
    this.initialize = function() {
      self.on('reset', self.onReset);
      oldInitialize.apply(this, arguments);
    };
    Backbone.Collection.call(this, models, options);
  },
  onReset: function() {
    this.setActive(this.first());
  },
  setActive: function(active, options) {
    var cid = active;
    if ( active instanceof Backbone.Model ) {
      cid = active.cid;
    }
    this.each(function(model) {
      model.set('current', model.cid === cid, options);
    });
  },
  getActive: function() {
    return this.find(function(model) {
      return model.get('current');
    });
  },
  next: function() {
    return this.at(this.indexOf(this.getActive()) + 1);
  },
  prev: function() {
    return this.at(this.indexOf(this.getActive()) - 1);
  }
});

它可能并不完美,但对我有用。希望它至少能让你走上正轨。以下是我的使用方法:

var someOtherCollection = App.Collection.extend({
    model: MyModel
});

关于javascript - 主干 - 尝试使用集合中的下一个或上一个模型重新渲染 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18478156/

相关文章:

javascript - Node.JS + MySQL - 结合定义变量和选择数据的查询

javascript - Marionette 进行 self 渲染的最佳方式

backbone.js - 如何使用 View 实现 Backbone 样板

javascript - model.save 设置 url 并在成功时回调

backbone.js - require.js 在部署时将所有模板压缩为一个模板

javascript - 如何获取不和谐 channel 的创建者?

javascript - Django 模板 - 如果 object.val == true 则设置复选框被选中

c# - 在图像按钮上滚动页面 Div 也向下滚动

javascript - 由于 CORS,HTTP post 请求失败

javascript - Lo-Dash,数组和集合的区别