假设我有一个网站,其中包含一些字段,我想对其进行一些计算.我有一个数量输入,并在我的backbone.js视图中我将"更改"事件绑定到此输入.
如何获取输入字段的更改值?
我发现这个方法(下面)获取值,但感觉不对,因为我必须在我的函数中再次知道元素ID.例如,我不能用课来做这件事.
window.Calculations = Backbone.View.extend({
(...)
events: {
'change input#quantity': 'changeQuantity'
},
changeQuantity: function() {
var val = $(this.el).find('input#quantity').val();
this.model.set({'quantity': val});
}
});
Run Code Online (Sandbox Code Playgroud)
在HTML中:
<input type="text" id="quantity" value="<%= quantity %>">
Run Code Online (Sandbox Code Playgroud)
我接近这个吗?是否有一个变量可以访问已更改的对象?我知道$(this.el)不是它,它只是容器.
使用react-router-redux,似乎获取路由信息的唯一方法是仅通过props.这是正确的吗?
这是我现在在应用程序中所做的大致内容:
<Provider store={Store}>
<Router history={history}>
<Route path="/" component={App}>
<Route path="child/:id" />
</Route>
</Router>
</Provider>
Run Code Online (Sandbox Code Playgroud)
应用
const App = (props) =>
<div className="app">
<Header />
<Main {...props}/>
<Footer />
</div>
Run Code Online (Sandbox Code Playgroud)
主要
const Main = (props) =>
<div>
<MessageList {...props}/>
</div>
Run Code Online (Sandbox Code Playgroud)
将MessageList
let MessageList = (props) => {
const {id} = props;
// now I can use the id from the route
}
const mapStateToProps = (state, props) => {
return {
id: props.params.id
};
};
MessageList = connect(mapStateToProps)(MessageList)
Run Code Online (Sandbox Code Playgroud)
我会喜欢 …
我使用Backbone.js作为框架,在我看来,我有一些小图像(垃圾桶可以删除,图标等).当视图重新渲染这些图像时闪烁.
我通过为我不想闪现的所有内容创建一个新视图来解决这个问题,而不是触发它们渲染.但是我想知道是否还有另一种方法可以做到这一点而不会将我的观点分解成一堆碎片?
这是我呈现我的观点的一般格式:
window.SomeView = Backbone.View.extend({
initialize: function() {
this.model.bind('change', this.render, this);
this.template = _.template($('#view-template').html());
},
render: function(){
var renderedContent = this.template(this.model.toJSON());
$(this.el).html(renderedContent);
return this;
},
events: { 'click .delete' : delete },
delete: function(ev){
//do delete stuff here
},
});
Run Code Online (Sandbox Code Playgroud)
然后我将它们附加到div:
var newView = new PopupItemImgView({model: someModel});
$('#styleimage').append(newView.render().el);
Run Code Online (Sandbox Code Playgroud)
我的模板可能看起来像
<script type="text/template" id="view-template">
<%
print('Content content - <img src="images/delete.gif" class="delete">');
%>
</script>
Run Code Online (Sandbox Code Playgroud)