gpt4 book ai didi

javascript - 主干听定制集合?

转载 作者:行者123 更新时间:2023-12-03 11:29:01 25 4
gpt4 key购买 nike

// build a collection of players with this team ID
this.collection = new PlayersCollection(App.data.players.where({team_id: this.model.id}));

当我这样做时,它会渲染 this.collection = App.data.players; 它与作为新集合的集合有关,我如何收听新集合?

使用 marionette,我将一组玩家传递到一组具有 collectionViews、compositeViews 和 itemViews 的团队中。

团队 Collection View > 团队复合 View > 玩家项目 View

在团队合成 View 中,我将 this.collection 定义为与特定团队 ID 匹配的新 PlayersCollection。这呈现完美,但添加新播放器后,我无法在不刷新页面的情况下重新呈现 View 。

addPlayer: function(e) {
e.preventDefault();

var $newPlayer = this.$el.find('input.player_name');

var Player = {
player_name : $newPlayer.val(),
team_id : this.model.id
}

App.data.players.create(Player);
$newPlayer.val('');

},

这里是大部分代码。我正在尝试添加/创建一个新播放器并使用 listenTo 更新 View ,但由于这不是定义集合的传统方式,看来我必须找到一种绑定(bind)它的方法?

Teams.js:

var Marionette = require('backbone.marionette'),
playerView = require('./player'),
PlayersCollection = require('../../collections/players');

var teamView = Marionette.CompositeView.extend({
events: {
'submit #AddPlayer': 'addPlayer'
},
initialize: function() {
// anytime something within this specific team changes, render
this.listenTo(this.model, 'change', this.render);

// build a collection of players with this team ID
this.collection = new PlayersCollection(App.data.players.where({team_id: this.model.id}));

},
addPlayer: function(e) {
e.preventDefault();

var $newPlayer = this.$el.find('input.player_name');

var Player = {
player_name : $newPlayer.val(),
team_id : this.model.id
}

App.data.players.create(Player);
$newPlayer.val('');

},
itemView: playerView,
appendHtml: function(collectionView, itemView){
collectionView.$('.the-players').append(itemView.el);
}
});

module.exports = CollectionView = Marionette.CollectionView.extend({
initialize: function() {
this.listenTo(this.collection, 'change', this.render);
},
itemView: teamView
});

Players.js:

var Marionette = require('backbone.marionette');

module.exports = playerView = Marionette.ItemView.extend({
className: 'players-wrap row',
template: require('../../../templates/teams/player.hbs'),
events: {
'mouseenter .player-image': 'playerOn',
'mouseleave .player-image': 'playerOff',
'click .player-image': 'playerClicked',
'keydown': 'on_keypress',
'click .one-point': 'onePoint',
'click .two-point': 'twoPoint',
'click .three-point': 'threePoint'
},
initialize:function() {
_.bindAll(this, 'on_keypress');
$(document).bind('keydown', this.on_keypress);
this.listenTo(this.model, 'change', this.render);
this.$el.attr('data-player', this.model.get('player_name').replace(/\s+/g,"_").toLowerCase());
window.play = this.model;
},
playerOn:function(e) {
this.$el.addClass('hover');
this.findPlayer(this.model.get('player_name'), 'add');
},
playerOff:function() {
this.$el.removeClass('hover');
this.findPlayer(this.model.get('player_name'), 'remove');
},
playerClicked:function() {
this.$el.toggleClass('hover-clicked');
this.findPlayer(this.model.get('player_name'), 'toggle');
},
findPlayer:function(name, action) {
var player = $('.data-wrap .player-data[data-player='+ name.replace(/\s+/g,"_").toLowerCase() +']');
if(action === 'toggle') { player.toggleClass('show-confirmed-clicked') }
if(action === 'add') { player.addClass('show-player-data') }
if(action === 'remove') { player.removeClass('show-player-data') }
},
onePoint:function(e) {
var addStat = parseInt(this.model.get('points')) + 1;
this.model.set('points', addStat);
this.model.save();
},
twoPoint:function(e) {
var addStat = parseInt(this.model.get('points')) + 2;
this.model.set('points', addStat);
this.model.save();
},
threePoint:function(e) {
var addStat = parseInt(this.model.get('points')) + 3;
this.model.set('points', addStat);
this.model.save();
},
on_keypress:function(e) {
this.keyStat(e, 49, 'points', 1); // 1 point
this.keyStat(e, 50, 'points', 2); // 2 points
this.keyStat(e, 51, 'points', 3); // 3 points
this.keyStat(e, 82, 'rebounds', 1); // R rebounds
this.keyStat(e, 83, 'steals', 1); // S steals
},
keyStat:function(e, keyVal, stat, number) {
if(e.keyCode == keyVal && !e.ctrlKey) {
if(this.$el.hasClass('hover')) {
var addStat = parseInt(this.model.get(stat)) + number;
this.model.set(stat, addStat);
this.model.save();
}
}
},
templateHelpers:function(){
return {
name_format: this.model.get('player_name').replace(/\s+/g, '_').toLowerCase()
}
}
});

最佳答案

我找不到您将新模型添加到 CompositeView 集合中的位置?我只发现 App.data.players.create(Player); 这不是 CompositeView 的集合。

如果App.data.players是另一个Backbone.Collection,您需要与远程服务器保持同步,因此您使用create来持久化它到服务器,然后像这样更改 addPlayer :

addPlayer: function(e) {
e.preventDefault();

var $newPlayer = this.$('input.player_name');

var Player = {
player_name : $newPlayer.val(),
team_id : this.model.id
}

var newPlayer = App.data.players.create(Player);
this.collection.add(newPlayer);
$newPlayer.val('');

},

CompositeView 将捕获集合的“add”事件并追加新的 itemView。请尝试让我知道是否有帮助。

请注意:检查您的代码后,我发现可以使用值为 的 itemViewContainer 属性删除 Teams.js 中的 appendHtml .the-players 这将提高渲染性能。

关于javascript - 主干听定制集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26810512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com