2012-09-01 83 views
1

如何將名稱變量從路由器傳遞給FriendInfocollection?每當我瀏覽到本地主機/應用/人/ MYNAME的console.log返回本地主機404 /超薄/ index.php文件/人/Backbone.js將變量從路由器傳遞給集合

有人點我在正確的方向上,因此使用具有收藏分享這個變量正確的網址?

<!DOCTYPE html> 
<html> 
<head> 
    <title>I have a back bone</title> 
</head> 
<body> 
    <button id="add-friend">Add Friend</button> 
    <button id='view-friends'>View Friends</button> 
    <ul id="friends-list"> 
    </ul> 
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> 
    <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script> 
    <script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script> 
<script> 
Friend = Backbone.Model.extend({ 
    name: null, 
    age: null, 
}); 
FriendDetailModel = Backbone.Model.extend(); 
FriendInfoModel = Backbone.Model.extend({ 
    name: null 
}); 
FriendDetailCollection = Backbone.Collection.extend({ 
    url: '../slim/index.php/people/', 
    model: FriendDetailModel 

}); 
FriendInfoCollection = Backbone.Collection.extend({ 
    model: FriendInfoModel, 
    url: '../slim/index.php/person/' + name, 
}); 


Friends = Backbone.Collection.extend({ 
    initialize: function(models, options) { 
     this.bind("add",options.view.addFriendLi); 
    } 
}); 
AppView = Backbone.View.extend({ 
    el: $("body"), 
    initialize: function() { 
     this.friends= new Friends(null, {view:this}); 
    }, 
    events: { 
     "click #add-friend": "showPrompt", 
    }, 
    showPrompt: function() { 
     var friend_name = prompt("Who is your friend?"); 
     var friend_age = prompt("What is your friends age?"); 
     var friend_model = new Friend({name: friend_name, age: friend_age}); 

     this.friends.add(friend_model); 
    }, 
    addFriendLi: function(model) { 
     $("#friends-list").append("<li>" + model.get('name') + " " + model.get('age') + "</li>"); 
    } 
}); 
var appview = new AppView; 
var people = new FriendDetailCollection; 
var person = new FriendInfoCollection({name:'Sean'}); 

FriendView = Backbone.View.extend({ 
    el: $("body"), 
    initialize: function() { 
     _.bindAll(this,'render'); 
     this.collection.bind('reset', this.render); 
    }, 
    events: { 
     "click #view-friends": "fetch_list", 
    }, 
    render: function() { 

     var results = this.collection.toJSON(); 
     $.each(results, function(key, value) { 
      var msg = results[key]['firstname'] + " " + results[key]['lastname']; 
      console.log(msg); 
     }); 
    }, 
    fetch_list: function() { 
       people.fetch({ 
         success: function(data) { 
           //console.log("success"); 
         } 
       }); 

    } 

}); 

FriendInfoView = Backbone.View.extend({ 
     el: $("body"), 
     initialize: function(name) { 
       _.bindAll(this,'render'); 
       this.collection.bind('reset', this.render); 
     }, 
     render: function(name) { 
       var results = this.collection.toJSON(); 
       $.each(results, function(key, value) { 
         var msg = results[key]['firstname'] + " " + results[key]['lastname']; 
         console.log(msg); 
       }); 
     } 

}); 


friendview = new FriendView({collection: people}); 
friendinfoview = new FriendInfoView({collection: person}); 
AppRouter = Backbone.Router.extend({ 
    routes: { 
     "friends":"people", 
     "person/:name":"personDetail" 
    }, 

    people: function() { 
     console.log('all the people'); 
     people.fetch({ 
      success: function(data) { 
       //console.log("success"); 
      } 
     }); 

    }, 

    personDetail: function(name) { 
     person.fetch({ 
         success: function(data) { 
           console.log("success"); 
         } 
       }); 

     console.log('one person named ' + name); 
    } 
}); 
var approuter = new AppRouter; 
Backbone.history.start(); 
</script> 
</body> 
</html> 
+1

兩件事** ** 1這句話是不可理解的,我_「每當我瀏覽到localhost/app/person/MyName console.log返回一個404爲localhost/slim/index.php/person /「_ ** 2。**請盡力在最小代碼表達式中重現問題,只是複製/粘貼您的整個應用程序代碼並讓我們開展工作以消除噪音並不是很禮貌。 – fguillen

回答

2

你應該定義路由器是這樣的:

var AppRouter = Backbone.Router.extend({ 
    routes: { 
     'name/:name'    : 'getPerson' 
    }, 
getPerson: function(name) { 
    var callperson = new Person.Details({ 
     name  : name 
    }); 
} 

實例化路線DOM準備好時

app_router = new AppRouter; 

現在您可以訪問名字像這樣在您的看法:

this.options.name 

但是你必須定義th初始化函數中選擇E可變

initialize: function (options) 

而且可以肯定,你可以設置模型是這樣的:

model.set('name', name) 
+0

他在詢問關於將參數傳遞給collection的問題,爲什麼你正在回答如何獲取? – YuC

+0

你能展示你的代碼是什麼意思嗎? –

+0

假設我們有一個名爲XXX的集合,並且它被兩個服務使用,所以我們需要爲不同的服務使用不同的url。例如。/api/a爲第一個服務,/ api/b爲第二個服務,所以url函數應該是這樣的: 'url:function(){ if(this.type ==='a'){ return'/API/A'; } else { return'/ api/b'; } }' 那麼如何在新的集合時將類型參數傳遞給集合? – YuC

相關問題