Build a JavaScript-based mobile cms--Build blog (ii.). Routing

Source: Internet
Author: User

After having the basis of the upper part. We will be able to generate a blog content--blogposts Detail. This is the end of our mobile CMS almost basic functionality, with the last section presumably for us to get an article is not a difficult thing.

Get each blog post

So we tiger to write a blogdetail.js

define ([' jquery ', ' underscore ', ' mustache ', ' text!/blog_details.html '],function ($, _, mustache, Blogdetailste mplate) {var Blogpostmodel = Backbone.Model.extend ({name: ' Blog Posts ', url:function () {Retu        RN This.instanceurl;        }, Initialize:function (props) {this.instanceurl = props;    }    }); var Blogdetailview = Backbone.View.extend ({el: $ ("#content"), Initialize:function () {}, G            Etblog:function (slug) {url = "http://api.phodal.net/blog/" + slug;            var = this;            collection = new Blogpostmodel;            Collection.initialize (URL);                Collection.fetch ({success:function (collection, response) {That.render (response);        }            });        }, Render:function (response) {this. $el. HTML (mustache.to_html (blogdetailstemplate, response));    }    }); return blogdetailview;}); 

Wrote another one blog_details.html . And then. And then

<div class= "Information pure-g" >    {{#.}    } <div class= "Pure-u-1" >        <div class= "L-box" >            

We obviously need to change a little bit before blog.html . In order to enable him to jump at the front desk

{{#.}} 

The question arises, how can we enter the final page?

Add a route to a blog post

After the end of the previous article, each blog post has a corresponding URL, that is, the corresponding slug. And our blog access is based on this URL, get, in other words, these things are done by the API. The thing to do here is to get the content of the blog and render it. Another problem with this is that Ajax-run data cannot be removed from the outside. Then there is the Getblog () call to render method above.

Backbone Routing Parameters

We need to pass in a number to tell Blogdetail which blog post to get.

    Routes: {        ' index ': ' homepage ',        ' blog/*slug ': ' blog ',        ' *actions ': ' homepage '    }

*slug is the content of the reference here, and then we need to call Getblog (slug) to process it.

App_router.on (' Route:blog ', function (blogslug) {        var blogdetailsview = new Blogdetail ();        Blogdetailsview.getblog (Blogslug);    });

Finally, our router.js " as seen below:

define ([    ' jquery ',    ' underscore ',    ' backbone ',    ' Homeview ',    ' blogdetail '], function ($, _, Backbone, Homeview, blogdetail) {    var approuter = Backbone.Router.extend ({        routes: {            ' index ': ' homepage ', c8/> ' blog/*slug ': ' blog ',            ' *actions ': ' homepage '        }    };    var initialize = function () {        var app_router = new Approuter;        App_router.on (' Route:homepage ', function () {            var homeview = new Homeview ();            Homeview.render ();        });        App_router.on (' Route:blog ', function (blogslug) {            var blogdetailsview = new Blogdetail ();            Blogdetailsview.getblog (Blogslug);        });        Backbone.history.start ();    };    return {        initialize:initialize    };});

Then we will be able to open each blog very happily to see what's inside.

Other

CMS effect: Ink qi cms

QQ Discussion Group: 344271543

Project: Https://github.com/gmszone/moqi.mobi

Build a JavaScript-based mobile cms--Build blog (ii.). Routing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.