2016-12-12 4 views
-1

Backbone.jsでは、URL形式を指定し、リダイレクト先のページを指定するためにルータを使用します。例えばbackbone.jsでURLを暗号化する方法

Backbone.history.navigate('#/serviceslist/1456/false/1111', { trigger: true }); 

リンクは次のようになります:

http://localhost:64865/#/serviceslist/1456/false/1111 

我々はJSで呼び出す

"serviceslist/:id/:serviceId": "serviceslist", 

serviceslist: function(id, serviceId) { 
    var self = this; 
    this.load(); 
    require([ 
     'js/views/patient/serviceslist' 
    ], function(serviceslist) { 
     self.shell(); 
     if (typeof app.serviceslist !== 'undefined') { 
      app.serviceslist.destroy(); 
     } 
     app.serviceslist = new serviceslist({ Id: id, fromouter: false, serviceId: serviceid }); 
     $("#main-nav li.active").removeClass("active"); 
     $("#admin").addClass("active"); 
    }); 
}, 

この部分のURLを暗号化する方法はありますか:1456/false/1111

+0

バックボーンはクライアント側であるため、操作が完了する前にすべてが既にユーザーの手元にあるので、暗号化は無意味です。あなたは本当に暗号化を意味し、暗号化を意味しませんか? –

答えて

0

エスケープする必要がある経路部分にencodeURIComponentを使用してください。あなたはしかし、ルータ上で、それをデコードする必要があるかもしれません

var id = '1456/false'; 
var serviceId = '1111'; 

Backbone.history.navigate('#/serviceslist/' + encodeURIComponent(id) + '/' + encodeURIComponent(serviceId), { trigger: true }); 

例:

console.log(encodeURIComponent('Time &time=again')); 
 
console.log('#/serviceslist/' + encodeURIComponent('1456&123/456') + '/false/1111')

だからあなたのナビゲートのコードは可能性があり

serviceslist: function (id,serviceId) { 
    // decode the route params :id & :serviceId 
    id = decodeURIComponent(id); 
    serviceId = decodeURIComponent(serviceId); 
    // the rest of your logic here 
} 
+0

このメソッドは、encodeURIComponentはint値を暗号化しません:S – Lili

+1

ああ...私は..あなたがそれをユーザーから "隠す"方法が必要であると思う。あなたは他の方法でこれを難読化する必要がありますが、最終的にはその敏感なIDですか?ユーザーがIDを推測しようとしないようにする必要がある場合は、別のものを使用する必要があります。たとえば、サービスのIDではなく、サービスの名前に基づいて生成された文字列「slug」を使用します。例:my-aswsome-service-one、my-awesome-service-two。これらは一意である必要がありますので、レコードを取得してIDを見つけることができます。もちろん、バックエンドに格納されています – Andy

+0

@リリこの答えは暗号化ではないエンコード方法を提供します。 –

関連する問題