2012-10-05 2 views
1

안녕하세요, 제 젠드 응용 프로그램에서 백본 응용 프로그램/자산 폴더를 만들고 싶습니다. 어디에 배치 할 것입니까? 백본 응용 프로그램 폴더는 간단히 말해서 Javascript,Stylesheets and TemplatesBackbone.js와 함께 Zend Framework

구성 백본 레일 URL이에있는 경우

class EntriesController < ApplicationController 
    respond_to :json 

    def index 
     respond_with Entry.all 
    end 
end 

백본 수집은 단순히 이해 항목/지표/항목에 대한

routes.rb 

    resources :entries 

레일 컨트롤러를 작동하는 방법이다 이 코드로

class Blog.Collections.Entries extends Backbone.Collection 
    url: '/entries' 

백본 라우터

class Blog.Routers.Entries extends Backbone.Router 
    routes: 
    '': 'index' 
    'entries/:id': 'show' 

    initialize: -> 
    @collection = new Blog.Collections.Entries() 
    @collection.fetch() 

    index: -> 
    view = new Blog.Views.EntriesIndex(collection: @collection) 
    #alert 'hi' 
    $('#container').html(view.render().el) 
    show: (id) -> 
    alert "Entry #{id}" 

는 그 다음

<h1>Blog</h1> 

<ul> 
<% for entry in @entries.models: %> 
    <li><%= entry.get('name')%></li> 
<% end %> 
</ul> 

마지막으로 자산/자바 스크립트와 응용 프로그램을 초기화하기

index.jst.eco/자산/템플릿/항목을 렌더링하기 위해 Backbone.View

class Blog.Views.EntriesIndex extends Backbone.View 

    template: JST['entries/index'] 

    initialize: -> 
     @collection.on('reset',@render,this) 
    render: -> 

     $(@el).html(@template(entries: @collection)) 
     this 

템플릿 렌더링/blog.js.coffee

window.Blog = 
    Models: {} 
    Collections: {} 
    Routers: {} 
    Views: {} 
    initialize: -> 
    new Blog.Routers.Entries() 
    Backbone.history.start() 

$(document).ready -> 
    Blog.initialize() 

위의 Backbone.js의 MVC 패턴은 흥미 롭습니다.하지만 Zend Framework로 구현하고 싶습니다. 그래서 어떤 설정을 레일스에서 ​​사용하는 것처럼 변경해야합니다. 또한 뷰를 렌더링하기 위해 템플릿을 처리하는 방법 ? 감사합니다

+1

질문이 명확하지 않습니다. Backbone은 RESTFul 서비스와 함께 작동하도록 설계된 클라이언트 측 라이브러리입니다. 백엔드에서 REST 서버를 구현하는 것은 불가 지합니다. 정확히 무엇을 요구하고 있습니까? –

+0

Zend Application에서 어떻게 사용합니까? 나는 자바 스크립트가 클라이언트 측이라는 것을 알고 있지만, 모두 스크립팅 언어 내에서 구현 방법을 가지고있다. 대부분 Backbone.js는 Rails와 함께 사용됩니다. 하지만 젠드 프레임 워크에 어떻게 포함 시켰는지 알고 싶습니다. – d3bug3r

+0

.. 그리고 레일즈에서 어떻게하는지 설명하는 이유는 무엇입니까? – tasmaniski

답변

0

당신의 공용 폴더 (모든 CSS와 자바 스크립트 파일이있는)에 백본 응용 프로그램을 넣을 수 있습니다.

선호하는 템플릿 엔진을 사용할 수있는 템플릿의 경우, 언더 코어를 사용하여 다른 라이브러리를 포함하지 않아도됩니다. 이것의 예는 다음과 같습니다 모델에서

window.product_v = Backbone.View.extend({ 
    template: _.template("<article><img src='<%=icon%>'/> <% } %><article>"), 

    render: function() { 
     var self = this; 
     this.model.fetch({ 
      processData: true, 
      success: function (m) { 
       var prod = $("<div/>", { 
         'html': self.template(m.toJSON()) 
       }).appendTo('.content'); 

      } 
     }); 
    } 
}); 

window.product_m = Backbone.Model.extend({ 
    urlRoot: '/api/get_product' 
}); 

은 당신이 당신의 젠드 컨트롤러 중 하나에, 아마도, 경로를 참조 할 곳이다 "UrlRoot에"당신의 데이터 소스를 정의하는 것입니다.