2014-01-06 5 views
2

我中存儲與火力自動火力每個崗位生成的ID。讓說,這-JBuEJXgILubWjxECjwj重命名URL ID爲可讀格式angularjs

然後我查詢後這樣

Post.getPost($routeParams.postID); //snippet code

所以當用戶訪問example.com/userid/-JBuEJXgILubWjxECjwj將顯示內容。但我希望URL可讀。

當用戶訪問example.com/userid/mystoryexample/userid/-JBuEJXgILubWjxECjwj會得到正確的崗位。

如何將ID重命名爲標題的網址是什麼?

UPDATE

基本上,這是如何在火力

posts: { 
    randomID1: { 
    title: 'This is post 1' 
    }, 
    randomID2: { 
    title: 'This is post 2 
    } 
} 

然後,我的數據存儲,我查詢使用angularFire的JavaScript API($火力)

$scope.post = $firebase(new Firebase.child('posts/').child($routeParams.postID)) 

我覺得查詢代碼是正常的,只是我的數據存儲爲我將id轉換爲title。 有什麼想法?

+0

燦你讓IDS和URL蛞蝓的鍵值存儲,使用slug任何導航和查找的ID在店鋪時,你需要得到一個帖子? – imcg

+0

@imcg你能給我舉個例子嗎? – vzhen

+0

我不熟悉的火力點,但是如果你發佈的一些代碼,你使用我會盡力。 – imcg

回答

2

這裏是如何的標題轉換爲URL蛞蝓,然後查找該ID(小提琴:http://jsfiddle.net/rvWhV/):

var posts = { 
    randomID1: { 
    title: 'This is post 1' 
    }, 
    randomID2: { 
    title: 'This is post 2' 
    } 
}; 

// find id from slug in posts 
function getPostId(slug) { 
    for(var i in posts) { 
    // match id or slug so both URL forms are supported 
    if(i === slug || posts[i].slug === slug) { 
     return i; 
    } 
    } 
    return null; 
} 

// create slugs and add them to post objects 
for(var i in posts) { 
    var p = posts[i]; 
    // create slug by replacing characters in title. TODO: handle posts with same title, maybe by appending a number 
    p.slug = p.title.replace(/\W/g, '').toLowerCase(); 

    // make a link to the post 
    var link = document.createElement('li'); 
    link.innerText = p.title; 
    link.setAttribute('id', p.slug); 
    link.onclick = function() { 
    var slug = this.getAttribute('id'); 
    alert('slug:' + slug + ' id:' + getPostId(slug)); 
    }; 
    document.body.appendChild(link); 
} 

要與路由器使用它,這樣做:

var postId = getPostId($routeParams.slug); 
$scope.post = $firebase(new Firebase.child('posts/').child(postID)); 
+0

這很複雜。我會跳過第一。當我在那裏時,會讓你知道。 – vzhen

+0

是的,我相信它在應用程序的上下文中要複雜得多。祝你好運! – imcg

+0

這假定你已經下載了所有的帖子fron firebase,對不對? –