2014-01-14 5 views
1

CORS을 사용 중이므로 모든 API가 api.mywebsite.com에서 발생하지만 웹 사이트는 website.com을 통해 제공됩니다.Backbone.js 기본 경로 (하위 도메인)

내가 또는 Backbone 중 하나의 설정을 사용하여 항상 AJAX 요청을 내 api.mywebsite.com으로 만들 수 있는지 궁금합니다.

url: '/books' 

및 자동 api.mywebsite.com/v1/books

답변

0

:

YourApp.Collections.Books = Backbone.Collection.extend({ 
    model: YourApp.Models.Book, 
    url: 'http://api.mywebsite.com/v1/books/' 
}); 

그리고 개별 자원에 대한 당신은 URL을 생성하는 기능을 사용할 수 있습니다 :

YourApp.Models.Book = Backbone.Model.extend({ 
    url: function() { 
    return 'http://api.mywebsite.com/v1/books/' + this.get('id') 
    } 
}); 
0

백본 모든 요청의 루트를 설정하는 UrlRoot에 방법을 가지고 추론이 : otherwords에서

, 내 백본 컬렉션에서이 작업을 수행 할 수 있습니다.

백본 사이트에서

에게 :

var Book = Backbone.Model.extend({urlRoot : '/books'}); 
var solaris = new Book({id: "1083-lem-solaris"}); 
alert(solaris.url()); 

경고 "/ 책/1083-LEM-솔라리스"당신이 뭔가 다른 그 상대 경로를 변경할 수 있습니다 중 코스

:에 앞에 점을 넣어 사이트 루트 또는 특정 사이트 루트로 보내거나 절대 경로를 지정하십시오. 당신은 당신이 AJAX 요청에 원하는대로 할 수있는 URL을 구축 할 수 jQuery를에 물론

Documentation

+0

당신이 나에게 예를 줄 수를 하위 도메인을 한 번 지정하고 모든 모델에서이를 사용하는 방법은 무엇입니까? – 0xSina

0

.

$.ajax({ 
    type: "GET", 
    url: "http://api.mywebsite.com + "anything_you_want_to_add" 
}).done(function(response) { 
    console.log(response); 
    receiveResponseMethodSomewhereElse(response); 
}); 
0

그리고 모델의 URL 매개 변수 또는 컬렉션 사용의 빠른 백본 예 :

의 I가 사용되는 스크립트를 보여주는 :

을 한 또 다른 도메인 아니다으로 절대 URL에 포함
<title>Backbone Test</title> 
<meta charset="UTF-8"> 
<script src="jquery.js"></script> 
<script src="underscore.js"></script> 
<script src="backbone.js"></script> 

그리고 나서이 백본 예제를 내 localhost에서 구체적으로 지적했습니다. 귀하의 도메인을 가리 키길 원할 것입니다. 디버깅 즐거움을 위해 모든 콘솔 로그와 사용 가능한 응답을 포함했습니다. 두 가지 옵션은 책 ID에서 URL을 작성하는 것으로, 서버에서 작성, 읽기, 갱신 또는 삭제하는 것이 정상입니다. 나는이 스크립트를 페이지 본문에 직접 넣고 콘솔 로그를 보았다. 참고 : 백본에서 JSON 응답을 기다리고 있습니다.

<script> 

     var Book = Backbone.Model.extend({urlRoot: 'http://localhost/url_test'}); 

     var BookCollection = Backbone.Collection.extend({ 
      model: Book, 
      url: 'http://localhost/url_test' 
     }); 

     var myExcellentBook = new Book({id: "book"}); 

     var MyBooks = new BookCollection(); 

     // getting it directly from the model 
     solaris.fetch({ 
      success: function(model, response, options) { 
       console.log("SUCCESS"); 
       console.log(model); 
       console.log(response); 
       console.log(options); 
      }, 
      error: function(model, response, options) { 
       console.log("ERROR"); 
       console.log(model); 
       console.log(response); 
       console.log(options); 
      }, 
      complete: function(xhr, textStatus) { 
       console.log(textStatus); 
      } 
     }); 

     // or fetch directly from the collection and 
     // normally you'd loop through the response or 
     // when creating new models, you can let backbone 
     // intialize them through the response 
     MyBooks.fetch({ 
      success: function(model, response, options) { 
       console.log("SUCCESS"); 
       console.log(model); 
       console.log(response); 
       console.log(options); 
      }, 
      error: function(model, response, options) { 
       console.log("ERROR"); 
       console.log(model); 
       console.log(response); 
       console.log(options); 
      }, 
      complete: function(xhr, textStatus) { 
       console.log(textStatus); 
      } 
     }); 
    </script> 

url_test의 PHP 스크립트는 단순히 JSON 객체를 반환합니다. 당신은 전체 URL을 지정할 수있는 컬렉션

<?php 
echo '[{"id": "MyNewBook"}]';