我将列表传递给视图.我的阵列看起来像这样:[football, basketball, soccer].在我看来,我想展示这样的东西:
1. football
2. basketball
3. soccer
Run Code Online (Sandbox Code Playgroud)
这意味着我必须遍历传递给数组的列表,对元素执行for循环.我该如何在模板文件中执行此操作?
对于每个元素,我必须得到它的索引并添加1.现在,我不知道如何在视图中执行此操作.我想这样做的一种方法是制作一本字典,就像这样{'1', 'football': '2', 'basketball': '3', 'soccer'},但由于我已经有了列表格式的数据,我宁愿不转换它.
我需要将模型的属性呈现给JSON,以便将它们传递给模板.以下是视图的render()函数:
render: function() {
console.log(this.model);
console.log(this.model.toJSON());
$(this.el).html(this.template(this.model.toJSON()));
return this;
},
Run Code Online (Sandbox Code Playgroud)
这是执行console.log(this.model)后输出的属性:
created_at: "2012-04-19"
id: "29"
name: "item"
resource_uri: "/api/v1/item/29/"
updated_at: "2012-04-21"
user: "/api/v1/user/9/"
Run Code Online (Sandbox Code Playgroud)
这是执行console.log(this.model.toJSON())后模型的JSON输出:
id: "29"
__proto__: Object
Run Code Online (Sandbox Code Playgroud)
发生了什么?
编辑:这是实例化:
var goal = new Goal({id: id});
goal.fetch();
var goalFullView = new GoalFullView({
model: goal,
});
Run Code Online (Sandbox Code Playgroud)
以下是新视图的内容:
console.log(this.model.attributes);
console.log(this.model.toJSON());
Run Code Online (Sandbox Code Playgroud)
这是控制台说的:
Object
created_at: "2012-04-23"
id: "32"
name: "test"
resource_uri: "/api/v1/goal/32/"
updated_at: "2012-04-23"
user: "/api/v1/user/9/"
__proto__: Object
Object
id: "32"
name: "test"
__proto__: Object
Run Code Online (Sandbox Code Playgroud)
如果toJSON应该复制属性,为什么不复制正确的名称或为什么不复制created_at,updated_at字段?
编辑2:这是模型:
var Goal = Backbone.Model.extend({
// Default attributes …Run Code Online (Sandbox Code Playgroud) 我有以下触发器:
CREATE TRIGGER sum
AFTER INSERT
ON news
FOR EACH ROW
UPDATE news SET NEW.sum = (NEW.int_views + NEW.ext_views)/NEW.pageviews
Run Code Online (Sandbox Code Playgroud)
它对表的列int_views和ext_views列进行求和,并将它们除以总页面浏览量.
每当我尝试向新闻添加新行时,我都会收到以下错误:
ERROR 1442 (HY000) at line 3: Can't update table 'news' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Run Code Online (Sandbox Code Playgroud)
触发对我来说似乎很简单.触发器无法运行是否有原因?
当我在ghci中做一些简单的事情时,如下所示:
let x = 7 + 2
Run Code Online (Sandbox Code Playgroud)
我希望ghci给出x所持类型的响应,如:
x :: Integer
Run Code Online (Sandbox Code Playgroud)
当我运行ghci时,我没有得到上述那一行.我该如何得到答复?
当使用Tastypie创建新项目时,我希望能够将其添加到用户的属性,该属性是多对多字段.现在我的obj_create看起来像这样:
def obj_create(self, bundle, request=None, **kwargs):
return super(GoalResource, self).obj_create(bundle, request, user=request.user)
Run Code Online (Sandbox Code Playgroud)
我想创建新对象,但是当我希望能够将它添加到request.user的属性goal_list时.但是,我所拥有的将立即在数据库中创建对象.如何创建对象然后将其添加到用户的goal_list属性?
我在我httpd.conf的重定向中使用以下代码:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
Run Code Online (Sandbox Code Playgroud)
但是,它最终会重定向到 www.domain.com/domain.com//domain.com//domain.com
我希望以下所有网址都重定向到domain.com:
http://domain.com
http://www.domain.com
www.domain.com
Run Code Online (Sandbox Code Playgroud) 将此代码放在足以保护页面免受未经身份验证的用户的路径中?
if (!req.user) return res.send(401, "Not allowed in");
Run Code Online (Sandbox Code Playgroud) 我有骨干视图,将动作绑定到DOM中的元素.问题是在调用事件时尚未呈现DOM元素.我的代码看起来像这样:
// Change the focus to a full goal view
var GoalFullView = Backbone.View.extend({
// Bind the goal full view
el: $("#main"),
// Events for making changes to the goal
events: {
"keypress #goal-update": "createOnEnter",
},
// Cache the template for the goal full view
template: _.template($("#goal-full-template").html()),
// Render the goal full and all of its goal updates
initialize: function() {
this.render();
this.collection = new GoalUpdateList;
this.collection.bind('add', this.addOne, this);
this.collection.bind('reset', this.addAll, this);
this.collection.fetch();
},
Run Code Online (Sandbox Code Playgroud)
如您所见,事件绑定发生在初始化之前,因此它无法定位DOM元素,因为它尚未呈现.如何在初始化之后延迟事件绑定?
编辑1:嗯,我做了一个控制台日志,它在按键上输出了一些东西,但是我在Chrome的开发者控制台中收到了这条消息:
Uncaught TypeError: Cannot call …Run Code Online (Sandbox Code Playgroud) 如果它们不存在,我想自动将密钥添加到Python字典中.例如,
a = "a"
b = "b"
c = "c"
dict = {}
dict[a][b] = c # doesn't work because dict[a] doesn't exist
Run Code Online (Sandbox Code Playgroud)
如果密钥不存在,如何自动创建密钥?
javascript ×3
backbone.js ×2
django ×2
python ×2
apache ×1
express ×1
ghci ×1
haskell ×1
html5 ×1
mysql ×1
node.js ×1
passport.js ×1
tastypie ×1
webrtc ×1