故事:我有很多需要设置的属性.由于它们有相似之处,我选择读出类,输入框是属性名称的一部分.
问题:可以设置对象的动态属性,就像关联数组一样.所以我能做到
var customattribute = this.$el.parents('div').attr('id');
var value = $(e.currentTarget()).val();
this.model.attributes[customattribute] = value;
Run Code Online (Sandbox Code Playgroud)
但这不会触发模型的更改事件.我可以手动触发更改事件,但这不会更新this.model.changedAttributes(),我只需要设置更改的属性,而不是每个属性.
这当然也不起作用:
this.model.set(customattribute: value);
Run Code Online (Sandbox Code Playgroud)
那我该如何处理这个问题呢?
我有可以设置的ALOT(200+)属性,我不想为每个属性创建单独的eventlisteners,除非这是唯一的方法.
码:
var Display = Backbone.View.extend({
className: 'display',
events: {
'slide .slider' : 'sliderHandler'
},
initialize: function(){
_.bindAll(this, 'render', 'sliderHandler','update');
this.model.on('change',this.update, this);
},
render: function(){
this.$el.html(_.template(html, {}));
this.$('.slider').slider();
return this;
},
sliderHandler: function(e){
var slider = $(e.currentTarget);
var property = slider.parents('div').attr('id');
var value = slider.slider('value');
this.model.attributes[property] = value;
},
update: function(){
console.log(this.model.changedAttributes());
//get changed attribute + value here
},
});
Run Code Online (Sandbox Code Playgroud)
编辑:
下面的两个答案解决了它.将属性映射到对象并将其提供给Backbone.我也发现了另一个解决方案 …
我正在尝试使用igraph预先计算稳定力导向图的位置,并将它们传递到我的d3.js图中.这是由于我将使用的数据集的大小,这意味着如果在客户端完成全力计算,我不能依赖客户端不冻结.我有JSON格式的位置,并使用线性比例,以使它们在d3.js中有用.
var positions =
{"positions": [{"x":"-68.824367374", "y": "-6.10824525755"},
{"x":"-80.8080803911", "y": "-3.38997541264"},
{"x":"6.75334817585", "y": "-49.6040729697"},
{"x":"14.6608797291", "y": "-81.8897019921"},
....
var force = d3.layout.force()
.charge(-100)
.linkDistance(3)
.size([width, height])
.nodes(data.nodes)
.links(data.links)
.start();
var posX = d3.scale.linear()
.range([0,960])
.domain([-125,120]);
var posY = d3.scale.linear()
.range([0,500])
.domain([-125,125]);
Run Code Online (Sandbox Code Playgroud)
这就是我试图这样做的方式.我已经尝试过px和py,但结果是一样的.好像这段代码从未运行过.如果我扔到console.log下面看到的地方,则不会打印该值.无论我在何处放置此代码,无论是在启动力之前还是之后.
force.on("start", function() {
node
.data(positions.positions)
.attr("cx", function(d) {
console.log(posX(d.x));
return posX(d.x);
})
.attr("cy", function(d) {
return posY(d.y);
})
});
Run Code Online (Sandbox Code Playgroud)
为什么on start事件没有设置我的节点的初始位置?它们似乎仍然是随机初始化的.或者,什么是预先计算d3.js力导向图的稳定状态的好方法?我曾经看过在Phantomjs上做过但是放弃了.
感谢您抽出宝贵时间阅读我的问题!
编辑
这是一个愚蠢的例子:https://jsfiddle.net/xp0zgqes/ 如果你运行它几次并注意节点的起始位置,你可以看到它们是随机初始化的.
我正在尝试在Windows 10 64位上设置OpenSSL,按照说明到目前为止,在安装Visual Studio之后,我试图使用Visual c ++ 2008命令提示符在openssl目录中进行nmake以获取以下错误:
"C:\Strawberry\perl\bin\perl.exe" "-I." -Mconfigdata "util\dofile.pl" "-omakefile" "crypto\include\internal\bn_conf.h.in" > crypto\include\internal\bn_conf.h
"C:\Strawberry\perl\bin\perl.exe" "-I." -Mconfigdata "util\dofile.pl" "-omakefile" "crypto\include\internal\dso_conf.h.in" > crypto\include\internal\dso_conf.h
"C:\Strawberry\perl\bin\perl.exe" "-I." -Mconfigdata "util\dofile.pl" "-omakefile" "include\openssl\opensslconf.h.in" > include\openssl\opensslconf.h
ias -d debug -ocrypto\aes\aes-ia64.obj "crypto\aes\aes-ia64.asm"
'ias' is not recognized as an internal or external command,
operable program or batch file.
NMAKE : fatal error U1077: 'ias' : return code '0x1'
Stop.
Run Code Online (Sandbox Code Playgroud)
如果我尝试编译32位版本,则此错误不同.这导致引用NASM的错误|我试图安装而没有运气.
有人可以提出解决方案吗?
感谢您抽出宝贵时间阅读我的问题.
javascript ×2
arrays ×1
backbone.js ×1
c++ ×1
d3.js ×1
force-layout ×1
igraph ×1
object ×1
openssl ×1
perl ×1
visual-c++ ×1
windows ×1