2012-12-04 69 views
1

我正在四處尋找的JavaScript路由庫,我來到薩米,所以我正在學習它。如何設置sammy的基礎網址?

是我見過的所有例子到目前爲止顯示熱點繼續從一個域作爲基本URL的路由,像www.mydomain.com/#然後將所有路線的推移

但我做我的本地目錄中嵌套目錄內的一些實驗,比如/ wwwroot文件/播放/森/所以我的基本URL將

http://localhost/~rockdeveloper/play/sammy/# 

,然後所有路由必須繼續下去,如:

http://localhost/~rockdeveloper/play/sammy/#/products 
http://localhost/~rockdeveloper/play/sammy/#/clients 
http://localhost/~rockdeveloper/play/sammy/#/search 

是有任何wa Ÿ設置這個基地網址,所以我可以繼續配置這樣的薩米路線?

get('#/products') 
get('#/clients') 
get('#/search') 

現在我必須到主字符串拼接的路線,我希望它會比這更聰明......

baseurl='/~rockdeveloper/play/sammy/#/search'; 
get(baseurl + '#/products'); 

感謝。

回答

2

Sammy是一個前端框架,你不需要提供baseurl,因爲所有的調用都是在url的基礎上在客戶端進行的,井號後面的路徑的其餘部分在那裏僅供sammy使用。

例:

http://localhost/~rockdeveloper/play/sammy/#/products 
http://localhost/~rockdeveloper/play/sammy/#/products/iphone 
http://localhost/~rockdeveloper/play/sammy/#/products/iphone/cases/all 

您的基本路徑是

http://localhost/~rockdeveloper/play/sammy/ 

在所有的人。其餘的只是sammy路線,(把它們想象成GET參數)

此外,您的路線必須以可以從錨點無縫呼叫的方式定義。下面是我的路線的一部分文件:


app = $.sammy('body', function() { 
    //define events 
    this.bind('addNew',function(e,data){ 
     //data.name = data.name.split(' ').join('_'); 
     for(x in mapa[data.element]){ 
     if(mapa[data.element][x].name.toLowerCase() == data.name.toLowerCase()){ 
       return alert('There is already an element with the same name'); 
      break; 
     } 
    } 
    ap('Adding: '+data.element+' with the name: '+data.name); 
    mapa[data.element].push({name:data.name}); 
    this.trigger('sections',{action:data.element}); 
    }); 


    // define routes 
    this.get('#/', function() { 
    $('#menuright').html(''); 
    $('.customMenu').remove(); 
    $('#holder').html('').attr({style:''}); 
    }); 

    this.get('#/someroute/:variable', function() { 
     /* 
     ... 
     ... 
     ... 
     */ 
    }); 

    this.before(function(){ 
    if(typeof(app.historial)=='undefined'){ 
     app.historial = []; 
    } 
    app.historial.unshift(this.path.split('#')[1]); 
    if(app.historial.length>2) app.historial.length = 2; 
    do_something(); 
    }); 
}); 

從你的觀點,你可以只是簡單的使用類似:

<a href="#/someroute/{{name}}"><i class="icon-bookmark"></i> {{name}}</a> 
//le me here using mustache :{