我已经使用构建了一个Web应用程序backbone.marionette
.从a开始Marionette.ItemView
,我触发事件document.location.hash
:
document.location.hash = '#tasks/' + this.model.get('id');
Run Code Online (Sandbox Code Playgroud)
1.a)它改变URL 1.b)它触发appRoutes
如果我Routing.navigate
从同一个地方触发:
router.navigate('#tasks/' + this.model.get('id'))
Run Code Online (Sandbox Code Playgroud)
2.a)它按预期更改URL 2.b)它不会触发appRoutes.
知道为什么2.b会发生吗?问题在哪里?
谢谢.
var Router = Marionette.AppRouter.extend({
appRoutes: {
'tasks': 'tasks',
'tasks/:id': 'taskDetail',
'*defaults': 'tasks'
}
});
Run Code Online (Sandbox Code Playgroud) 我正在使用bootstrap-dropdown生成下拉菜单.我想在点击它时防止菜单消失.
我已经实现了以下代码,但它不起作用.知道怎么解决吗?
<li class="dropdown notification">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<span class="notification-label label label-info">
<i class="icon-inbox icon-white"></i>
<span class="notification-number">{{ unread_notifications }}</span>
</span>
</a>
<ul class="dropdown-menu notification-list"></ul>
</li>
Run Code Online (Sandbox Code Playgroud)
events: {
'click ul.notification-list': 'onClickNotification'
},
onClickNotification: function (e) {
e.preventDefault();
console.log(e);
},
Run Code Online (Sandbox Code Playgroud) 我们假设有以下html结构(1).
从$('.child') element
我可以访问$('.gran-parent') element
制作类似的东西$('.child').parent().parent();
.
无论如何,我不认为这是一个好方法因为不是通用的.
我们假设$('.gran-parent')和$('.child')之间还有其他div.
什么是引用第一个父类的最通用的方法gran-parent
,从哪个类开始$('.child')
?
<div class='gran-parent'>
<div class='parent'>
<div class='child'>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 查看引导库我发现在使用.on函数时,
事件是以这种方式编写的:
$(document).on('click.button.data-api', '[data-toggle^=button]', function (e) {
// some code
})
Run Code Online (Sandbox Code Playgroud)
写这个事件的目的是什么click.button.data-api
而不是click
?
是否click
足够?
我想重新渲染一个列表,使用Marionette.ItemView
相应的模型进行ItemView
更改.
有关激活此任务的最佳方法的任何想法?
// Collection passed to Marionette.CompositeView
myCollection.attributes = [
{
id: 1,
name: 'bar'
closed: false
},
….
];
Run Code Online (Sandbox Code Playgroud)
// Marionette.ItemView
myModel.set({
closed: true
}); // when this model change I would like to re-render the copositeView
// or remove the Marionette.ItemView
Run Code Online (Sandbox Code Playgroud)
PS:
如果我尝试下面的代码(1)Marionette.ItemView
,
当我保存模型时,我得到以下错误(2).
(1)
// Marionette.ItemView
initialize: function () {
this.model.on('change', this.render);
}
Run Code Online (Sandbox Code Playgroud)
(2)
Uncaught TypeError: Object [object Object] has no method 'serializeData'
Run Code Online (Sandbox Code Playgroud) 假设我想使用jQueryUi在具有表单的backboneView中实现自动完成.
我实现了以下代码(*),但我不喜欢它,因为当用户没有输入任何字母时也会执行集合的提取.
仅当用户开始在输入框中键入内容时,我应该如何执行提取集合?
var MyView = Backbone.View.extend({
initialize: function () {
this.myCollection = new MyCollection();
this.myCollection.fetch(); // I would like to fetch the collection
// only when the user start to type the first letter
},
events: {
'focus #names': 'getAutocomplete'
},
getAutocomplete: function () {
$("#names").autocomplete({
source: JSON.stringify(this.myCollection)
});
}
});
Run Code Online (Sandbox Code Playgroud)
PS:
当用户键入第一个字母时,应该只执行一次提取.
我正在使用Marionette.CompositeView
,我想基于这两个例子(1)和(2)理解serializeData和onRender之间的区别.
根据文档,在应用模板之前在渲染中调用serializeData,并在应用模板后在渲染中调用onRender.
我的问题是:
1)为什么示例(1)有效,而(2)没有?
2)如果我重置集合,是否Marionette.CompositeView
会重新渲染?
有关详细信息,请参阅代码中的注释.
(1)
return Marionette.CompositeView.extend({
initialize: function () {
this.collection = new MyCollection();
this.collection.fetch();
},
onRender: function () {
this.collection.length > 0 ? this.$el.show() : this.$el.hide();
// it returns this.collection.length > 0
// differently from serializeData.
}
});
Run Code Online (Sandbox Code Playgroud)
(2)
return Marionette.CompositeView.extend({
initialize: function () {
this.collection = new MyCollection();
this.collection.fetch();
},
serializeData: function () {
this.collection.length > 0 ? this.$el.show() : this.$el.hide();
// it returns this.collection.length = 0
// even if this.collection.length …
Run Code Online (Sandbox Code Playgroud) 通过使用ajax我可以访问XHR对象简单地:
$.ajax().fail(function (XHR) {
// some code
});
Run Code Online (Sandbox Code Playgroud)
保存骨干模型时:
var MyView = Backbone.View.extend({
saveModel: function () {
this.myModel.save({
error: this.onError
});
}
onError: function (xhr) {
// how to access xhr?
}
});
Run Code Online (Sandbox Code Playgroud)
保存backbone.model
onError服务器事件时,如何获取XHR ?
使用jquery
,underscore
或者纯粹javascript
就是删除指定字符串的第一个字符的最佳方式?
我将做这样的事情:
"aamerica".substring(1,"aamerica".length);
"aamerica".slice(1);
Run Code Online (Sandbox Code Playgroud)
最好使用slice或substring?
我使用较少,以使背景颜色透明的div.
这是我的代码,它不适用于IE8:
background-color: fade(@mycolor, @transparency);
Run Code Online (Sandbox Code Playgroud)
我的问题是:
什么是最好的方法,因为我使用较少,以获得与IE8相同的效果?
javascript ×9
backbone.js ×5
jquery ×5
marionette ×3
ajax ×1
css ×1
html ×1
jquery-ui ×1
less ×1
mixins ×1