我不确切地知道错误/是在哪里。
我正在做一个单页应用程序,这是上下文:
到目前为止,这是我的代码:
// app/routes.php
Route::group(['prefix' => 'v1'],function (){
Route::resource('users','V1\UsersController');
Route::get('/', function()
{
return View::make('layouts.application')->nest('content', 'app');
});
});
// app/controllers/V1/UsersController.php
namespace V1;
//import classes that are not in this new namespace
use BaseController;
use User;
use View;
use Input;
use Request;
class UsersController extends \BaseController {
public function index()
{
return $users = User::all(['id','email','name']) ;
}
public function show($id)
{
$user = User::find($id,['id','email','name']);
if(is_null($user)){
return array(
'id' => 0,
'email' => 'fake email'
);
}
return $user;
}
// public/js/namespaces.js
(function(){
window.App = {
Models : {},
Collections : {},
Views : {},
Router : {}
};
window.Templator = function (mustacheView){
return $.ajax({
url: '/views/'+mustacheView,
async : false,
type: 'GET',
}).responseText;
};
window.Vent = _.extend({},Backbone.Events);
})();
// public/js/backbone/router.js
App.Router = Backbone.Router.extend({
routes : {
'' : 'home',
'users' : 'showAll',
'users/:id' : 'showUser',
'login' : 'showLoginForm'
},
home: function (){
Vent.trigger('home:index');
},
showAll : function (){
Vent.trigger('users:showAll');
},
showUser: function (id){
Vent.trigger('users:show',id);
},
showLoginForm : function (){
Vent.trigger('login:form');
}
});
// public/js/app.js
$(function() {
$(document).on("click", "a", function(e){
var href = $(this).attr("href");
if (href != '#') {
e.preventDefault();
Backbone.history.navigate(href,{trigger:true});
}
});
new App.Views.AllUsers;
new App.Views.Index;
new App.Views.Login;
new App.Router;
Backbone.history.start({
pushState: true,
silent: true,
root: '/v1'
});
});
所以,如果我输入这个URI" domain.dev/v1/users"在导航栏上,显示JSON中的用户列表,并且不显示与骨干相关联的视图。 有什么建议吗?
答案 0 :(得分:0)
看一下我刚才给出的关于Angular的类似问题的答案:Angular and Laravel
在文字中,只需在心理上将Angular
替换为Backbone
。
如何将路线从Laravel传递到Backbone:
从Laravel为'/'
路由返回的基本视图需要在<head>
之前的某个地方包含 backbone.js
之类的内容:
<script>
var myRoute = "{{ $route }}";
</script>
此刀片模板会创建一个Javascript变量。然后,在Backbone中声明路由后,添加:
Backbone.history.start(); // You should already have this line, so just add the next one
App.nav.navigate(myRoute);
这应该可以解决问题。