2013-12-09 6 views
1

필자는 백본에 Im과 require js를 사용하여 프로젝트를 만들었습니다. (필자는이 프로젝트가 실제로 처음 프로젝트 인 것을 요구합니다.만들기 JS 모듈, 백본 필요 만들기

지금. 내 home.js보기에 나는 "플러그인"forexample 하나 개의 분리 된 모듈에이 모든 기능을 넣어 내가 내 home.js에 필요한 함수를 호출 할이

iScroll: function() { 

       window.scrollers.myScroll = new iScroll(this.$('#products_container')[0], { 
        vScrollbar: true, 
        onScrollStart: function() {........... 

swipeMobile: function() { 
       $("#mobile_view").swipe({ 

        swipe: function (event, direction, distance, duration, fingerCount) { 
         if (direction == "left") {.... 

같은 많은 기능을 볼 때 I 필요해 .

임 로딩이

define([ 
     'text!templates/about.html', 
     'text!templates/home.html', 
     'text!templates/product.html', 
     'text!templates/contact.html', 
     'collections/products' 
    ], 

    function (aboutTemplate, homeTemplate, productTemplate, contactTemplate, Products) { 

같은 집보기 내 템플릿은 가장 쉬운 방법은이 작업을 수행하는 뭐죠?

답변

2

기능의 컬렉션은 백본 모델, 컬렉션, 또는보기의 확장 인 경우, 다음을 수행 할 수 있습니다

define(['backbone'], function(Backbone) { 
    return Backbone.View.extend({ 
     customMethod: function() { 
      return this; 
     } 
    }); 
}); 

가 그럼 그냥 파일의 경로를 필요로하고 당신과 같이 사용할 수 있습니다 :

require(['path/to/custom/view'], function(CustomView) { 
    var customview = new CustomView(); 
    customview.customMethod(); // returns the Backbone view 
}); 

편집 :

: 당신은 몇 가지 코드가있는 경우 는 모델, 컬렉션 또는보기, 대신이 작업을 수행 할 수 아니라고 (이없이 의존성이있는 모듈 있다고 가정)
define(function() { 
    var MyCustomHelper = function() { 
     this.someattribute = true; 
    }; 

    MyCustomHelper.prototype.customMethod = function() { 
     return this.someattribute; 
    }; 

    return MyCustomHelper; 
}); 
+0

감사합니다. Btw 내가 백본으로 작업하지 않는다면 어떻게 보일까요? 단순한 모듈을 필요로하는 페이지에 필요로합니다. – chipatama