给定包含多个数据元素(如对象或数组)的数据,是否可以使用单个值函数在选择上设置多个属性?
例如:
var data = [{ 'x': 10, 'y': 20, 'r': 5 }];
d3.select('body').append('svg').selectAll('circle')
.data(data)
.enter().append('circle')
.attr('cx cy r', function (d) {
return [d.x, d.y, d.r];
});
Run Code Online (Sandbox Code Playgroud)
代替:
var data = [{ 'x': 10, 'y': 20, 'r': 5 }];
d3.select('body').append('svg').selectAll('circle')
.data(data)
.enter().append('circle')
.attr('cx', function (d) {
return d.x;
});
.attr('cy', function (d) {
return d.y;
});
.attr('r', function (d) {
return d.r;
});
Run Code Online (Sandbox Code Playgroud) 我想分享如何捆绑一个充当插件主机的应用程序以及如何动态加载已安装的插件.
网上有几个人正在寻找这个问题的解决方案:
这里描述的解决方案基于@ sokra 2014年4月17日关于Webpack问题#118的评论,并稍微调整以适应Webpack 2. https://github.com/webpack/webpack/issues/118
要点:
插件需要一个ID(或"URI"),它在后端服务器上注册,并且对应用程序是唯一的.
为了避免每个插件的块/模块ID冲突,JSONP将使用单独的加载器函数来加载插件的块.
加载插件是由动态创建的<script>元素(而不是require())启动的,让主应用程序最终通过JSONP回调消耗插件的导出.
注意:您可能会发现Webpack的"JSONP"措辞具有误导性,因为实际上没有JSON转移,但插件的Javascript包含在"加载器功能"中.服务器端没有填充.
构建一个插件
插件的构建配置使用Webpack output.library和output.libraryTarget选项.
示例插件配置:
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/' + pluginUri + '/',
filename: 'js/[name].js',
library: pluginIdent,
libraryTarget: 'jsonp'
},
...
}
Run Code Online (Sandbox Code Playgroud)
插件开发人员可以为插件选择一个唯一的ID(或"URI"),并使其在插件配置中可用.在这里我使用变量pluginURI:
// unique plugin ID (using dots for namespacing)
var …Run Code Online (Sandbox Code Playgroud) 我想假冒CasperJS(/ PhantomJS)的Navigator平台属性.我找到了在页面加载时覆盖Navigator对象的简单解决方案,这在Web上的许多其他地方都是建议的:
casper.on('page.initialized', function(){
this.evaluate(function(){
(function(oldNav){
var newNav = {};
[].forEach.call(Object.getOwnPropertyNames(navigator), function(prop){
if (prop === 'platform') {
Object.defineProperty(newNav, prop, {
value: 'Win64'
}); }else {
Object.defineProperty(newNav, prop, {
get: function(){
return oldNav[prop];
}
});
}
});
window.navigator = newNav;
})(window.navigator);
});
});
Run Code Online (Sandbox Code Playgroud)
但问题是,如果我们从Iframe获取Navigator属性,则值仍然是原始值,因为page.initialized仅将其设置为主页面.所以我选择在源代码中更改它并再次构建它.我从git repo下载了Phantomjs,我搜索了一个硬编码的平台值(对于我的情况,Linux x86_64).我找到了硬编码的字符串./phantomjs/src/qt/qtwebkit/Source/WebCore/platform/qt/UserAgentQt.cpp
我把它改成了我想要作为navigator.platform返回的字符串,但它没有影响navigator.platform.我应该在哪里改变它?它(平台)是一个带编码的字符串还是动态创建的?
javascript ×3
attributes ×1
casperjs ×1
d3.js ×1
navigator ×1
overwrite ×1
phantomjs ×1
webpack ×1