2012-06-13 77 views
0

沒有更新HTML我的觀點:Backbone.js的在查看

class FoursquareSearch.Views.SearchNew extends Backbone.View 

    tagName: 'sidebar' 

    template: JST["templates/search/new_search"] 

    #events: 
    # 'submit': 'create' 

    initialize: -> 
    @render 

    render: -> 
    console.log('hello') 
    $this = $('#sidebar') 
    $this.html('<p>All new content. <em>You bet!</em></p>') 
    $this 

與此路由器

class FoursquareSearch.Routers.Maps extends Backbone.Router 

    routes: 
    '': 'index' 

    index: -> 
    FoursquareSearch.Views.maps = new FoursquareSearch.Views.Maps() 
    @model = new FoursquareSearch.Models.Map() 
    #originForm = new Traveltime.Views.ShapesOriginForm(model: model, map: Traveltime.Views.map) 
    newSearch = new FoursquareSearch.Views.SearchNew(model: model, map: FoursquareSearch.Views.maps) 

而這個HTML

<div id="top"></div> 
    <div id="sidebar"> 

    </div> 

初始化代碼:

window.FoursquareSearch = 
    Models: {} 
    Collections: {} 
    Views: {} 
    Routers: {} 
    init: -> 

    new FoursquareSearch.Routers.Maps() 
    Backbone.history.start() 

$(document).ready -> 
    FoursquareSearch.init() 

我可以看到console.log消息,但是HTML類/ id沒有得到更新!

如果我運行:

$this = $('#sidebar') 
$this.html('<p>All new content. <em>You bet!</em></p>') 

在控制檯中,我可以看到頁面上的HTML的變化,這似乎只是Backbone.js的不想要更新我的看法?

+0

請添加您的初始化代碼。你忘了調用'Backbone.history.start()'嗎?你能通過日誌記錄確認「index」實際上是否被調用? –

+0

我已經添加了init代碼,不確定如何確認索引是否在實際調用中?但它確實運行了console.log,正如我所說,所以我知道它通過渲染功能... –

回答

1

將@render更新爲您的初始化方法中的@render()。您只是簡單地返回該方法,但從不調用它。

+1

打敗我,所以你可以有這個演示:http://jsfiddle.net/ambiguous/xU7DQ/2/ –

+0

謝謝 - 但是,如果我改變它沒有區別,在例子@ muistooshort張貼有一個FoursquareSearch.init()在路由器的末尾?如果我添加這個,我得到一個錯誤:第12行的分析錯誤:意外的'IDENTIFIER'' –

+0

@CharlieDavies:jsfiddle將所有內容封裝到一個onload處理程序中,因此它不需要'$(document).ready(... )'東西。你在做什麼和我的小提琴在做什麼不同? –