我做了一个简单的实时访客计数器.
您可以从此存储库下载它.
发生的事情是从不触发服务器上的断开连接事件(即使在浏览器关闭之后).
server.js是:
(function () {
var app, count, express, io;
express = require('express');
io = require('socket.io');
app = module.exports = express.createServer();
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('stylus').middleware({
src: __dirname + '/public'
}));
app.use(app.router);
return app.use(express.static(__dirname + '/public'));
});
app.configure('development', function () {
return app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
app.configure('production', function () {
return app.use(express.errorHandler());
});
io = require('socket.io').listen(app);
count = 0;
io.sockets.on('connection', function (socket) {
count++;
io.sockets.emit('count', …Run Code Online (Sandbox Code Playgroud) 我无法在d3.js中使用强制布局显示节点标签.
我正在尝试这个例子http://d3js.org/d3.v3.min.js
我更新了只添加缩放的代码,如下所示:
var svg = d3.select("body").append("svg").attr("width", width).attr("height", height).append('svg:g').call(d3.behavior.zoom().on("zoom", redraw));
function redraw() {
console.log("here", d3.event.translate, d3.event.scale);
svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")");
node.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
}
Run Code Online (Sandbox Code Playgroud)
为什么不显示标签?
我的测试用例是:
describe('Test', function(){
beforeEach(function(){
browser().navigateTo('/index.html')
})
it('test 1', function(){
console.log('doc',document)
expect(true).toBe(true)
})
})
Run Code Online (Sandbox Code Playgroud)
我的业力配置文件是:
// Karma configuration
// Generated on Wed Oct 09 2013 17:04:44 GMT+0200 (CEST)
module.exports = function (config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath:'',
// frameworks to use
frameworks:['jasmine'],
// list of files / patterns to load in the browser
files:[
'test/*.js'
],
// list of files to exclude
exclude:[
],
// test results reporter to use
// …Run Code Online (Sandbox Code Playgroud) 假设我有Django模型的Category和Item,其中Item具有ForeignKey朝向Category(这个字段被命名为category),我在Backbone中创建了Item模型,如下所示:
defaults:{ name:'', category_id:''}
Run Code Online (Sandbox Code Playgroud)
但是当我保存模型一个项目我得到错误:
raise self.field.rel.to.DoesNotExist\n\nDoesNotExist\n
在django/db/models/fields/related.py中,似乎category_id字段不被识别为Item模型的字段.
我正在使用tastyapi,而ItemModel和ItemResource是:
class Item(models.Model):
id = models.AutoField(primary_key=True)
category=models.ForeignKey('categories.Category')
name = models.CharField(max_length=300)
class ProductResource(ModelResource):
category=fields.ForeignKey(CategoryResource,'category')
class Meta:
queryset= Product.objects.all()
resource_name='product'
authorization= Authorization()
Run Code Online (Sandbox Code Playgroud)
一些细节:当related.py文件执行val = getattr(instance, self.field.attname)self.field.attname是STORE_ID但isntance.store_id是无,即使骨干模型,为STORE_ID值,26.
一些帮助?
我有这棵对象树
A
B延伸A
C延伸B
D 延伸 B
E延伸C
F 扩展 A 并且有一个对 A 的引用
A 有以下注释
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS,include=JsonTypeInfo.As.PROPERTY,property="@class")
如果我尝试反序列化扩展 A 的对象的 JSON 数组,则会抛出以下错误
org.codehaus.jackson.map.JsonMappingException:意外的标记(START_OBJECT),预期的VALUE_STRING:需要包含类型id的JSON字符串(对于java.util.Collection的子类型)
json 字符串是由集合的 toString() 方法生成的,并且集合是类型 A 的参数,其中 A 使用以下代码在 JSON 中序列化:
ObjectMapper objectMapper=new ObjectMapper();
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS);
String res="";
try {
res = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(t);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res;
Run Code Online (Sandbox Code Playgroud)
反序列化 json 数组(即上面描述的集合)的代码是:
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_CONCRETE_AND_ARRAYS);
Collection<T> results=null;
try {
results = …Run Code Online (Sandbox Code Playgroud) 我有一个集合,我想通过计算其属性中的相同值来分组.所以我执行这个:
_.countBy(T.collection,function(model){
return model.get('text')
})
Run Code Online (Sandbox Code Playgroud)
其中attribute是一个字符串.该字符串可以包含字母(Az),':'和'_'(下划线).它没有空白.
但代码抛出
无法调用未定义的方法'get'.
我也尝试过
T.collection.countBy(function(model){
return model.get('text')
})
Run Code Online (Sandbox Code Playgroud)
但它会抛出
Object [object Object]没有方法'countBy'
我有一个Python字典,其中键是字符串,值是MyObject对象的列表.如果我执行
simplejson.dumps(dict)
Run Code Online (Sandbox Code Playgroud)
它抛出"MyObject not JSON serializable".
如何避免此异常,如何使MyObject可序列化?
我正在尝试使用 NodeJS/Express 提供压缩视图。
即使我正确配置了应用程序,也没有压缩的痕迹。仅压缩静态文件。
如果我在 Chrome 中访问视图,我找不到该字段Content-Encoding: gzip。
以下是我的 Express 应用程序的配置:
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.engine('html', ejs.renderFile);
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(expressValidator());
app.use(express.methodOverride());
app.use(express.staticCache());
app.use(gzippo.staticGzip( path.join(__dirname, 'public') ,{maxAge:86400000} ));
app.use(gzippo.compress());
//app.use(express.compress());
app.use(app.router);
Run Code Online (Sandbox Code Playgroud)
请注意,我使用 gzippo 进行压缩。然而基本的压缩也express.compress()不起作用。
我有这个字符串
q="""insert into genres (movieid,%(genre_name)s) values (%(movieid)i,1)""" % {'genre_name': t2, 'movieid': movieid}
Run Code Online (Sandbox Code Playgroud)
但结果print q是
)values(1,1)nres(movieid,adventure
代替
插入流派(movieid,adventure)值(1,1)
为什么?
我正在开发一个简单的ajax调用($ .ajax函数,类型,url和数据作为选项参数),在页面中使用jquery,但是我得到了403错误.所以我已经为$ .ajax函数添加了csrf的另一个选项参数,并且相应的标记在模板中正确显示.但我再次收到403错误.我也尝试添加到视图处理程序(在views.py中)一个csrf装饰器,像@csrf_protect或@requires_csrf_token没有任何改变.
一些帮助?
我已经看到这种模式在django中进行ajax调用https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#s-ajax
但是我做一个简单的ajax调用真是太多代码了,所以我找到了一个简短的解决方案
django ×2
javascript ×2
json ×2
node.js ×2
python ×2
ajax ×1
arrays ×1
backbone.js ×1
collections ×1
compression ×1
d3.js ×1
dictionary ×1
disconnect ×1
events ×1
exception ×1
express ×1
force-layout ×1
foreign-keys ×1
formatting ×1
frontend ×1
graph ×1
gzip ×1
http ×1
jackson ×1
jasmine ×1
java ×1
karma-runner ×1
labels ×1
methods ×1
model ×1
serializable ×1
simplejson ×1
socket.io ×1
string ×1
tastypie ×1
unit-testing ×1