新入职的公司需要掌握backbone.js, 因为js基础薄弱,出现了很多问题,在写backbone.js中的例子todos中将数据换成与后台返回的数据,对于url的使用不太明白,通过 https://github.com/rethinkdb/rethinkdb-example-flask-backbone-todo
才理解怎样使用,特此感谢!!!
在collection或者Model 曾添加url属相,如下:
var TodoList = Backbone.Collection.extend({
// Reference to this collection's model.
model: Todo,
// RethinkDB server
url: '/todos',
// Filter down the list of all todo items that are finished.
done: function () {
return this.filter(function (todo) {
return todo.get('done');
});
},
在需要获取的地方通过todos.fetch()去获取数据可以查看浏览器(按12)调用控制台进行查看url都进行了哪些请求并在后端中写入对应请求:
控制台显示调用的url.png@app.route('/todos', methods=["GET"])
def get_todos():
if not session.has_key("logged_in"):
return json.dumps({})
else:
selection = Comment.query.filter_by(user_id=session['currentuser'][0]).all()
dictsel = [];
for i in selection:
dictsel.append(Comment.serizale(i))
print dictsel
return json.dumps(dictsel)
@app.route('/todos', methods=["POST"])
def new_todo():
print request.json
com = Comment.query.filter_by(order=request.json.get('order'), user_id=request.json.get("user_id")).first()
# print com.get('order')
if com:
com.title = request.json.get('title')
com.done = request.json.get('done')
db.session.commit()
print '更新成功'
return jsonify(Comment.serizale(com))
else:
todo = Comment.new_todo(request.json)
print '添加成功'
return jsonify(Comment.serizale(todo))
@app.route('/todos/<string:todo_id>', methods=["PUT"])
def gput_todo(todo_id):
com = Comment.query.filter_by(order=todo_id).first()
if com:
com.title = request.json.get('title')
com.done = request.json.get('done')
db.session.commit()
print (u'更新成功')
return jsonify(Comment.serizale(com))
###deleting
@app.route("/todos/<string:todo_id>", methods=["DELETE"])
def delete_todo(todo_id):
com = Comment.query.filter_by(order=todo_id).first();
db.session.delete(com)
db.session.commit()
flash(u"删除成功")
return ''"
主要看代码标头部分,可以看出,请求响应对应去执行存储操作
如果文中出现什么错误的问题,欢迎指正!!!
------------------------------------------------结束分割线------------------------------------------------------------------------------
网友评论