我正在尝试在 OnChange 事件上获取 JSON。并对现有的haml页面进行更改。我的目标是用 json 值填充另一个选择字段。目前我只是想打印一条消息,说明 json 已获取。但我的 html 没有任何变化。
在控制台的网络选项卡中,我看到正在发出 URL 请求,并返回 304 响应。我还可以在响应中看到 json。
events:
"submit form" : "onSubmit"
"change [name=repository]" : "onChange"
onChange: (e) ->
e.preventDefault()
name = @$("[name=repository]").val()
localStorage['new_issue_last_repo'] = name
token = localStorage['oauth2_github']['accessToken']
@model = new Assignees(name)
console.log 'Printttttt'
@model.fetch {headers : @token},
success: (model) =>
@$('.assignee').html("<span>Fetched assignees</span>")
error: =>
@$('.assignee').html("<span>Failed to fetch assignees :(</span>")
haml 文件如下所示。
.message
.assignee
%form
%section.repo-select
%select{name: 'repository'}
- for repository in @repositories.models
- full_name = repository.get('full_name')
- if localStorage['new_issue_last_repo'] == repository.get('full_name')
%option{value: full_name, selected: 'selected'}= repository.get('full_name')
- else
%option{value: full_name}= repository.get('full_name')
获取 json 后如何让 .assignee 更改。另外我怎样才能访问json数据?
我有一个类似的功能。我不知道我在 onChange 函数中做错了什么。
onSubmit: (e) ->
e.preventDefault()
name = @$("[name=repository]").val()
localStorage['new_issue_last_repo'] = name
repository = @repositories.find (r) -> r.get('full_name') == name
model = new IssueModel({
body: @$("[name=body]").val()
title: @$("[name=title]").val()
assignee: @$("[name=assignee]").val()
milestone: @$("[name=milestone]").val()
}, {repository: repository})
model.save {},
success: (model) =>
@badge = new Badge()
@badge.addIssues(1)
@$('.message').html("<span>Issue <a href=\"#{model.get("html_url")}\" target=\"_blank\">##{model.get('number')}</a> was created!</span>")
error: =>
@$('.message').html("<span>Failed to create issue :(</span>")