- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这些称为节拍的 div,我想在单击它们时进行注册。但是,我似乎根本无法让他们注册点击,无论是通过点击它们,还是在控制台中的特定 div 上调用 JQuery click 事件。无论哪种方式,都没有注册。
measureView.js 创建这个 beatView,它在父度量中创建一个节拍。
beatView.js:
//filename: views/beats/beatView.js
/* This is the view for a single beat, which is contained in a measure view. */
define([ 'jquery', 'underscore', 'backbone', 'backbone/models/beat', 'text!backbone/templates/measures/audioMeasures.html', 'text!backbone/templates/beats/linearBarBeats.html', 'text!backbone/templates/beats/linearBarSVGBeats.html', 'text!backbone/templates/beats/circularPieBeats.html', 'app/dispatch', 'app/log'
], function($, _, Backbone, BeatModel, audioMeasuresTemplate, linearBarBeatsTemplate, linearBarSVGBeatsTemplate, circularPieBeatsTemplate, dispatch, log){
return Backbone.View.extend({
//registering backbone's click event to our toggle() function.
events : {
'click' : 'toggle'
},
//The constructor takes options because these views are created by measuresView objects.
initialize: function(options){
if (options) {
console.log('options :');
console.warn(options);
this.model = options.model;
// this.parentEl should be measure.cid
this.measureBeatHolder = options.parentElHolder;
} else {
console.log('never really getting here');
this.model = new BeatModel;
}
this.render();
},
//We use css classes to control the color of the beat. A beat is essentially an empty div.
render: function(toggle){
var state = this.getSelectionBooleanCSS();
if (toggle) {
$('#beat'+toggle).removeClass(state);
$('#beat'+toggle).addClass(this.switchSelectionBooleanValue());
} else {
var compiledTemplate = _.template(this.representations[this.currentBeatRepresentation], {beat: this.model, beatAngle: this.beatAngle, state: state});
$(this.measureBeatHolder).append( compiledTemplate );
return this;
}
},
getSelectionBooleanCSS: function(){
if (this.model.get("selected")) {
return "ON";
} else {
return "OFF";
}
},
switchSelectionBooleanValue: function(){
if (this.model.get('selected') == true) {
this.model.set('selected', "false");
} else {
this.model.set('selected', "true");
}
return this.model.get('selected');
},
/*
This is called when a beat is clicked.
It does a number of things:
1. toggles the model's selected field.
2. re-renders the beat.
3. prints a console message.
4. tells log to send a log of the click event.
5. triggers a beatClicked event.
*/
toggle: function(){
console.log('getting to toggle function');
var selectedBool = this.model.get("selected");
this.model.set("selected", !selectedBool);
var newBool = this.model.get("selected");
this.render(this.model.cid);
dispatch.trigger('beatClicked.event');
}
});
});
//filename: models/beat.js
/*
This is the beat model.
It only knows about whether or not it
is selected.
*/
define([
'underscore',
'backbone'
], function(_, Backbone) {
var beatModel = Backbone.Model.extend({
defaults: {
selected: false,
state: 'OFF'
},
initialize: function(){
},
getStyleClass: function() {
if (this.selected) {
return 'ON';
}
else {
return 'OFF';
}
}
});
return beatModel;
});
//filename: models/measure.js
/*
This is the measure model.
A component has a collection of these models.
these models have a collection of beats.
*/
define([
'underscore',
'backbone',
'backbone/collections/beats'
], function(_, Backbone, beatsCollection) {
var measureModel = Backbone.Model.extend({
defaults: {
label: '0/4',
beats: beatsCollection,
numberOfBeats: 0,
divisions: 8
},
initialize: function(){
}
});
return measureModel;
});
// Filename: views/measures/measuresView.js
/*
This is the MeasuresView.
This is contained in a ComponentsView.
*/
define([
'jquery',
'underscore',
'backbone',
'backbone/collections/measures',
'backbone/collections/beats',
'backbone/models/measure',
'backbone/views/beats/beatView',
'text!backbone/templates/measures/audioMeasures.html',
'text!backbone/templates/measures/linearBarMeasures.html',
'text!backbone/templates/measures/linearBarSVGMeasures.html',
'text!backbone/templates/measures/circularPieMeasures.html',
'app/dispatch',
'app/state',
'app/log'
], function($, _, Backbone, MeasureModel, BeatsCollection, MeasuresCollection, beatView, audioMeasuresTemplate, linearBarMeasuresTemplate, linearBarSVGMeasuresTemplate, circularPieMeasuresTemplate, dispatch, state, log){
return Backbone.View.extend({
// el: $('.component'),
// The different representations
representations: {
"audio": audioMeasuresTemplate,
"linear-bar": linearBarMeasuresTemplate,
"linear-bar-svg": linearBarSVGMeasuresTemplate,
"circular-pie": circularPieMeasuresTemplate
},
currentMeasureRepresentation: 'linear-bar',
//registering click events to add and remove measures.
events : {
'click .addMeasure' : 'add',
'click .delete' : 'remove'
},
initialize: function(options){
//if we're being created by a componentView, we are
//passed in options. Otherwise we create a single
//measure and add it to our collection.
if (options) {
this.measuresCollection = options.collection;
this.parent = options.parent;
this.el = options.el;
}
// else {
// this.measure = new BeatsCollection;
// for (var i = 0; i < 4; i++) {
// this.measure.add();
// }
// this.measuresCollection = new MeasuresCollection;
// this.measuresCollection.add({beats: this.measure});
// }
if (options["template-key"]) {
this.currentBeatRepresentation = options["template-key"];
}
//registering a callback for signatureChange events.
dispatch.on('signatureChange.event', this.reconfigure, this);
//Dispatch listeners
dispatch.on('measureRepresentation.event', this.changeMeasureRepresentation, this);
this.render();
//Determines the intial beat width based on the global signature. Has to be below this.render()
this.calcBeatWidth(this.parent.get('signature'));
},
changeMeasureRepresentation: function(representation) {
this.currentMeasureRepresentation = representation;
this.render();
},
render: function(){
$(this.el).html('<div class="addMeasure">+</div>');
var measureCount = 1;
//we create a BeatsView for each measure.
_.each(this.measuresCollection.models, function(measure) {
// when representation button changes, the current representation template will get updated
var compiledTemplate = _.template( this.representations[this.currentMeasureRepresentation], {measure: measure, beatHolder:"beatHolder"+measure.cid, measureCount:measureCount, measureAngle: 360.0 } );
$(this.el).find('.addMeasure').before( compiledTemplate );
console.log('measure beats: ');
console.warn(measure.get('beats').models);
_.each(measure.get('beats').models, function(beat) {
// console.warn("#beat"+beat.cid.toString());
new beatView({model:beat, parentElHolder:'#beatHolder'+measure.cid, parentCID:measure.cid, singleBeat:"#beat"+beat.cid});
}, this);
measureCount ++;
}, this);
return this;
},
/*
This is called when the user clicks on the plus to add a new measure.
It creates a new measure and adds it to the component.
It generates a string representing the id of the measure and the ids of
its beats and logs the creation.
Lastly, it triggers a stopRequest, because we can't continue playing until
all the durations get recalculated to reflect this new measure.
*/
add: function(){
console.log('add measure');
var newMeasure = new BeatsCollection;
for (var i = 0; i < this.parent.get('signature'); i++) {
newMeasure.add();
}
this.measuresCollection.add({beats: newMeasure});
//Logging
name = 'measure' + _.last(this.measuresCollection.models).cid + '.';
_.each(newMeasure.models, function(beats) {
name = name + 'beat'+ beats.cid + '.';
}, this);
log.sendLog([[3, "Added a measure: "+name]]);
//Render
this.render();
//Dispatch
dispatch.trigger('stopRequest.event', 'off');
},
/*
This is called when the user clicks on the minus to remove a measure.
*/
remove: function(ev){
if ($('#measure'+this.measuresCollection.models[0].cid).parent()) {
//removing the last measure isn't allowed.
if(this.measuresCollection.models.length == 1) {
console.log('Can\'t remove the last measure!');
return;
}
console.log('remove measure');
//we remove the measure and get its model.
var model = this.measuresCollection.get($(ev.target).parents('.measure').attr('id').replace('measure',''));
this.measuresCollection.remove(model);
//send a log event showing the removal.
log.sendLog([[3, "Removed a measure: measure"+model.cid]]);
//re-render the view.
this.render();
//trigger a stop request to stop playback.
dispatch.trigger('stopRequest.event', 'off');
dispatch.trigger('signatureChange.event', this.parent.get('signature'));
}
},
// This is triggered by signatureChange events.
reconfigure: function(signature) {
console.log('MeasureView.reconfigure(signature) : signature=' +signature);
/* if the containing component is selected, this
triggers a request event to stop the sound.
Then this destroys the beat collection and creates
a new collection with the number of beats specified
by the signature parameter.
*/
if ($(this.parent).hasClass('selected')) {
dispatch.trigger('stopRequest.event', 'off');
this.measure.reset();
for (var i = 0; i < signature; i++) {
this.measure.add();
}
//re-render the view.
this.render();
//recalculate the widths for each beat.
this.calcBeatWidth(signature);
dispatch.trigger('signatureChange.event', this.parent.get('signature'));
}
},
//This determines the width of each beat based on the
//number of beats per measure or 'signature'.
calcBeatWidth: function(signature) {
if ($(this.el).hasClass('selected')) {
var px = 100/$('.measure').css('width').replace(/[^-\d\.]/g, '');
var beatWidth = (100 - ((signature*1+1)*px))/signature;
$(this.el).children('.beat').css({
'width' : beatWidth+'%'
});
}
}
});
});
最佳答案
Backbone 带你的events
对象并将所有这些事件类型和选择器委托(delegate)给 View 的 el
.您希望事件注册的任何 HTML 都需要插入到 View 的 el
中。 ,以及 el
需要插入页面。
通常我这样设置我的 View :
var myView = Backbone.View.extend({
id: 'myView',
events: {
'click li' : 'myEventCallback'
},
initialize: function() {
$('body').append(this.el); //el on the page now
this.render(); //fills up el with useful markup
},
render: function() {
//fills el with useful markup from a template
this.el.html( JST['myTemplate' ]() );
},
myEventCallback: function() {
//code for handling click events on the li's inside the el of this view
}
});
关于backbone.js - Backbone 点击事件绑定(bind)未绑定(bind)到 DOM 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15768152/
我有一个主 View 负责呈现其他 View ...... 这是完整的代码 (1) (2) (3)。 当我第一次加载 View (View1、View2、View3)时,一切正常。 然后,如果我尝试重
我正在第一次尝试使用 Backbone.Marionette,并想知道当一个简单的 Backbone.View 就足够了时,是否有任何理由使用 Backbone.Marionette.ItemView
我正在尝试将 Backbone Validation 与 Backbone Stickit 结合使用,我希望在用户输入时一次验证一个属性。但是,当用户输入一个值时,模型上的所有属性都会得到验证,而不仅
我在结合使用 T. Hedersen 的 backbone.validation 插件 ( https://github.com/thedersen/backbone.validation ) 和 D
在下面编辑了这个 在下图中,我有两个主要区域。 左边的用户列表:allusersRegion 另一个用于显示布局的右侧,其中包含在 allusersRegion 中单击的用户的唯一属性和用户的文章列表
如果您单击链接,我的主干路由器工作正常,但在尝试直接访问 URL 或刷新页面时不起作用。 路由器 var app = app || {}; var appRouter = Backbone.Route
我想创建一个 Backbone 模型并将另一个模型的集合存储到它的属性中。所以,有父子模型。每个父级都有一个或多个子级存储在其属性中的数组中。 JSON 将是这样的。 Parent = { n
我正在使用 Backbone 和木偶, 我想对我的收藏和渲染 View 进行排序。 但是发生了一些奇怪的事情。 '/api/note/getList' ,它返回(并在集合被 View 初始化时调用)
我有一个相当通用的模型,并且正在收集该模型(请参见下文),作为一系列观点的基础。在几种 View 上,选择一个模型会生成操作(通过“selected”属性),我需要能够仅在客户端跟踪选择。 但是,似乎
这是一个单一的问题,但我对这是否是一个好习惯这一事实深有感触。 基本上,假设我们有这个微不足道的场景: (function(){ window.App = { Models: {},
我正在使用 Signalr 集线器订阅服务器上的事件。将什么事件分派(dispatch)到集线器,它成功地将项目添加到 Marionette CollectionView。反过来,这会呈现到表格中。
我正在使用 require js 和 Backbone 为 android 开发应用程序。我必须通过 touchend 事件将从集合中获取的模型传递给路由器。我该怎么做? define(["jquer
我有一个 Backbone 集合。如何对集合进行切片,或者至少将列表截断为特定长度? 最佳答案 假设您已经定义并初始化了您的集合,并且您想要改变集合(就地更改),您必须执行以下操作: collecti
我有一个集合,其中有一个添加模型时会触发的事件。我已经在文档中阅读了应该具有options参数但无法获取的参数。 我基本上想在集合中找到模型所在的索引。 在我的收藏夹中,我有这个。 initi
从集合中删除模型时,如何获取模型的索引。 在下面的代码中有一个回调函数 doSomething(){} ,它在 remove 被触发时被调用,我希望索引在那里。 Backbone 文档说“移除前模型的
在项目中同时使用Polymer和Backbone是否有任何限制? 我想不出什么具体的东西,但我想我会在匆忙之前先问清楚。有人可能已经同他们一起工作过,并且发现了某种不兼容之处。 欢迎任何反馈和经验分享
我正在使用bone.js编写应用程序,并在页面之间进行动画处理(有点像iPhone风格的ui)。因此,当您单击按钮时,下一页将从右侧滑入,而单击后退按钮将使下一页从左侧滑入。我希望能够使用路由器对浏览
我刚刚开始学习ribs.js。我在理解如何/何时使用模型和集合时遇到问题。我在网上找到了几个教程,每个教程都使用不同的方法来构建应用程序。在某些情况下,是从Collection对象的REST API中
我有一个由 REST API 给出的模型,其中一个名称的属性是这样的: defaults: { ... "user-name" : "", ... } 然后当我尝试通过这种方式在模板中呈现它时: U
我有一个 Backbone 应用程序,它从我使用 Backbone 的代码中的不同位置不确定地获取错误 Backbone is not defined 。有时它会先加载然后加载网站,有时则不会。我将以
我是一名优秀的程序员,十分优秀!