gpt4 book ai didi

Backbone.js 创建方法不向 sinatra 发送参数

转载 作者:行者123 更新时间:2023-12-03 23:32:45 26 4
gpt4 key购买 nike

我是主干的新手,并试图在 Sinatra 中进行设置,但我似乎无法让简单的创建工作。

我已经设置了我的模型/集合:

var TEAM_ID = window.location.pathname.split('/')[1]; // From url
$(function () {
var TeamMember = Backbone.Model.extend({
defaults: {
name : ""
}
});

var TeamMembers = Backbone.Collection.extend({
model: TeamMember,
url: "/" + TEAM_ID + "/team-members.json"
});

var teamMembers = new TeamMembers;

var TeamMemberView = Backbone.View.extend({
events: {
"click #new-team-member-form .submit-button" : "handleNewTeamMember"
},
handleNewTeamMember: function(data) {
var inputField = $('input[name=new_team_member_name]');
console.log("Pre create");

// This doesn't get sent to the server!!
var teamMember = teamMembers.create({name: inputField.val());

console.log("Post create");

return false; // Don't submit form
},
render: function() {
console.log("Render team member");

return this;
}
});

// ...

var teamMemberView = new TeamMemberView({el: $('#week-view')});

});

html 看起来像:
<table id="week-view">
<!-- ... -->
<form id="new-team-member-form" action="/some-add-url" method="post">
<fieldset class="new-object-fieldset" title="New team member">
<legend>New team member</legend>
<label for="new_team_member_name">Add new</label>
<input type="text" name="new_team_member_name" title="Add member" class="new-object-text-box" />
<button type="submit" name="new_team_member" value="new_team_member" class="submit-button">+</button>
<div id="help-new"></div>
</fieldset> <!-- New team member -->
</form>
<!-- ... -->

ruby 看起来像:
post '/:team_id/team-members.json' do  
logger.info("Add team member (json): #{params}")
end

但是sinatra服务器只显示 params[:team_id] ,没有 name teamMembers.create 上的参数线。我在做一些愚蠢的事情吗?没有正确初始化某些东西?

我看过 http://documentcloud.github.com/backbone/#Collection-create ,
http://documentcloud.github.com/backbone/docs/todos.html , http://liquidmedia.ca/blog/2011/01/backbone-js-part-1/ , http://liquidmedia.ca/blog/2011/01/an-intro-to-backbone-js-part-2-controllers-and-views/https://gist.github.com/1655019 ,但我似乎无法在那里找到任何答案。我觉得我做了一些愚蠢的事情,但就是看不到它!

最佳答案

事实证明,是我不知道如何正确提取 sinatra 中的 json 参数。来自本站:http://mini.softwareas.com/posting-json-to-a-sinatra-mongodb-service ,我发现我必须使用 request.body.read.to_s而不是 params哈希即,

post '/:team_id/team-members.json' do  
request_body = JSON.parse(request.body.read.to_s)
team_member_name = request_body["name"]
# ...
end

关于Backbone.js 创建方法不向 sinatra 发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9133798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com