运行的应用骨干作为一个独立的应用JS - 路线不工作 [英] Running an Backbone app as an independent JS application - Routes not working

查看:87
本文介绍了运行的应用骨干作为一个独立的应用JS - 路线不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我跑骨干为我的Rails 3.2的应用程序的前端。我需要将其迁移到一个独立的应用程序的JS,因为我把它作为Trigger.io的一部分的一部分。

现在存在作为自己index.html文件,参考资产的文件夹。

当我打开该文件,它加载骨干模块,但网页仍然是空的。而当我跑取()命令,它

所以,我有一对夫妇QNS的:


1)如何触发路由,使得它在默认情况下转到一个指定的页面?

我知道它得到Backbone.History.Start触发,但是我不知道在此之前,该怎么做。


2)的网址为文件://localhost/Users/mingyeow/Desktop/index.html

我如何手动设置的根URL来使用localhost:3000 /我的网址


解决方案

  //定义路由器
VAR路由器= Backbone.Router.extend({
  路线:{
    '指数':'的indexAction',
    *默认':'_defaultAction
  },
  的indexAction:功能(){
    //当用户浏览到这#INDEX将执行
  },
  _defaultAction:功能(){
    //这将被执行,如果用户浏览其他地方(#XXX)
  }
});//在文件准备就绪
$(函数(){
  //初始化路由器
  新的路由器();
  //并启动历史
  Backbone.history.start();
});

您可以浏览这样

; A HREF =#指数>指数路线和LT; / A>

&LT:通过单击链接


您可以使用Python服务器。要启动它的类型在终端:

  $蟒蛇-m SimpleHTTPServer

和检查的http://本地主机:8000

currently, I run backbone as the front-end of my rails 3.2 application. I need to migrate it into an independent JS application, as part of my putting it as part of Trigger.io.

It now exists as its own index.html file, referencing the assets folder.

When I open the file, it loads the Backbone modules, but the page remains empty. And when I run fetch() commands, it

So, I got a couple of qns:


1) How do I trigger the routes such that it goes to a designated page by default?

I know it gets triggered in Backbone.History.Start, but I am not sure what to do before that.


2) The url is "file://localhost/Users/mingyeow/Desktop/index.html"

How do I set the root url manually to use localhost:3000/my web address?

解决方案

// define router
var Router = Backbone.Router.extend({
  routes : {
    'index' : 'indexAction',
    '*default' : '_defaultAction'
  },
  indexAction : function() {
    // this will be executed when user navigate to #index
  },
  _defaultAction : function() {
    // this will be executed when user navigate anywhere else (#XXX)
  }
});

// on document ready
$(function() {
  // initialize router
  new Router();
  // and start history
  Backbone.history.start();
});

You can navigate this way.

Or by clicking the link : <a href="#index"">Index route</a>


You can use python server. To start it type in the Terminal:

$ python -m SimpleHTTPServer

And check http://localhost:8000

这篇关于运行的应用骨干作为一个独立的应用JS - 路线不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆