Backbone.js:视图或集合可以有属性吗?

发布于 2024-12-25 04:20:42 字数 245 浏览 0 评论 0原文

假设我有一个男孩和女孩名字的列表。该姓名集合还具有“性别”属性。

我想在集合或视图上放置“SelectedGender”属性,以便仅当所选性别发生更改时才可以触发自定义事件。

可以扩展视图以具有触发更改事件的属性吗?

可以扩展集合以具有触发更改事件的属性吗?

或者我是否被迫创建另一个包含集合的模型?我实际上没有代码,我现在只是在考虑设计。

Lets say I have a list of boys and girls names. This collection of names also has a 'Gender' property.

I want to put a 'SelectedGender' attribute on either the Collection or the View so that I can fire a custom event only when the Selected gender changes.

Can a View be extended to have a attribute that fires a change event?

Can a Collection be extended to have a attribute that fires a change event?

Or am I forced to create another Model that contains a collection? I don't actually have code I'm just thinking design at this point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤仙姿 2025-01-01 04:20:42

回答您的问题:是否可以扩展视图或集合以具有触发更改事件的属性,答案是否定的。

不,您不能执行 view.set({attr: value})collection.set({attr: value})

但是,您可以拥有视图或通过扩展 Backbone.Events 来收集火灾事件。

YourView = Backbone.View.extend({});
_.extend(YourView, Backbone.Events);

var view = new YourView();
view.bind('SelectedGenderChanged', function() {
  console.log('SelectedGenderChanged event fired!');
});

//somewhere else in your code
view.trigger('SelectedGenderChanged');

然而,可能有更好的设计方法。

如果当用户从组合框中进行选择时所选性别发生变化,那么您可以订阅视图中的组合框更改事件,然后让您的视图进行当时需要完成的任何更改。

To answer your question: Can a view or collection be extended to have an attribute that fires a change event, the answer is no and yes.

No, you can't do view.set({attr: value}) or collection.set({attr: value})

You can, however have a view or collection fire events by extending Backbone.Events.

YourView = Backbone.View.extend({});
_.extend(YourView, Backbone.Events);

var view = new YourView();
view.bind('SelectedGenderChanged', function() {
  console.log('SelectedGenderChanged event fired!');
});

//somewhere else in your code
view.trigger('SelectedGenderChanged');

However, there might be a better way to design this.

If the selected gender changes when a user makes a selection from a combo box, then you can subscribe to the combo box change event in your view, and then have your view make any changes that need to be done at that time.

滴情不沾 2025-01-01 04:20:42

我认为您不需要为您的集合创建包装模型。只需创建一个存储姓名和性别的模型即可。然后,就像@Paul提到的那样,您可以订阅集合或视图中的更改事件。

var nameModel = Backbone.Model.extend({
  defaults : function() {
    return {
      name   : "Andy",
      gender : "male"
    };
  }
} );

var nameCollection = Backbone.Collection.extend( {
  initialize : function ( args ) {
    var self = this;
    self.model.bind( "change:gender", function () {
      self.changeGender( self.model.get( "gender" ) );
    } );
  },

  changeGender : function( gender ) {
    // modify your collection somehow
  }
} );

var nameView = Backbone.View.extend( {
  initialize : function( args ) {
    var self = this;
    self.model.bind( "change:gender", function () {
      self.changeGender( self.model.get( "gender" ) );
    } );
    // in case you have a checkbox or something you want to bind the model change to:
    self.$( ".genderCheckbox" ).bind( "change", function( ev ) {
      self.model.set( { gender, self.$( ".genderCheckbox" ).val() } );
    } );
  },

  changeGender : function( gender ) {
     // Modify your view here
  }
} );

编辑

与其根据名字的性别拥有 2 个单独的集合,为什么不拥有一个仅返回男孩名字的方法和一个仅返回女孩名字的方法的名字集合呢?

IE:

getMaleNames : function() {
  return this.filter( function( name ) {
    return name.get( 'gender' ) == "male";
  } );
}

I don't think you need to create wrapper models for your collections. Just create a model that stores the name and the gender. Then, like @Paul mentioned, you can subscribe to the change event in the collection or in the view.

var nameModel = Backbone.Model.extend({
  defaults : function() {
    return {
      name   : "Andy",
      gender : "male"
    };
  }
} );

var nameCollection = Backbone.Collection.extend( {
  initialize : function ( args ) {
    var self = this;
    self.model.bind( "change:gender", function () {
      self.changeGender( self.model.get( "gender" ) );
    } );
  },

  changeGender : function( gender ) {
    // modify your collection somehow
  }
} );

var nameView = Backbone.View.extend( {
  initialize : function( args ) {
    var self = this;
    self.model.bind( "change:gender", function () {
      self.changeGender( self.model.get( "gender" ) );
    } );
    // in case you have a checkbox or something you want to bind the model change to:
    self.$( ".genderCheckbox" ).bind( "change", function( ev ) {
      self.model.set( { gender, self.$( ".genderCheckbox" ).val() } );
    } );
  },

  changeGender : function( gender ) {
     // Modify your view here
  }
} );

EDIT:

Rather than having 2 seperate collections depending on the gender of the name, why not have one collection of names with a method that returns just the boy names and a method that returns just the girl names?

ie:

getMaleNames : function() {
  return this.filter( function( name ) {
    return name.get( 'gender' ) == "male";
  } );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文