我想在OnChange事件上獲取JSON。並對現有的haml頁面進行更改。 我的目標是用json值填充另一個選擇字段。目前,我只是試圖打印一條json被提取的消息。但是我在html中沒有任何改變。將事件綁定到函數Coffeescript。 Backbone.js
在控制檯的網絡選項卡中,我看到正在創建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>")
@ muistooshort更新了問題。對於模糊的問題抱歉,我仍然試圖找出咖啡標記。 – shshank