美文网首页
自制简易前端MVC框架

自制简易前端MVC框架

作者: MMoooooon | 来源:发表于2017-03-29 16:49 被阅读0次

周末花了大概7小时写了一个简易的响应式blog,原意是练习css的,写着写着却去实现了一套前端路由并渲染的东西,这里写一点心得体会

基本思路与涉及技术

  1. 使用url hash进行路由跳转
  2. js监听hashchange事件,根据当前hash去决定界面如何渲染
  3. 调用 addHandler(hash, func)这个api来映射hash与handler
  4. gulp,scss, es6,模板引擎
  5. 需要一些es6的知识,需要理解this
  6. 整个工程在 https://github.com/MoonShining/front-end-hero/tree/master/blog, front-end-hero是我自己写的模板代码生成器,用它来练习CSS, 使用ruby create.rb -n name -s url来快速创建目录结构,免去重复的工作

例子

web.gif

核心代码

(()=>{

    let blog = new Blog()
    // add simple router
    blog.addHandler(['', '#programming'], function(){
        let data = {articles: [{title: 'stories to be continue', date: '2017-04-09'}]}
        this.compile('#article-template', data) 
    })

    blog.addHandler('#about', function(){
        let data = {avatar: 'http://7xqlni.com1.z0.glb.clouddn.com/IMG_0337.JPG?imageView2/1/w/100/h/100', name: 'Jack Zhou'}
        this.compile('#about-template', data)
    })

    // initialize the page
    blog.init()

})()

调用blog.addHandler来自定义路由改变之后触发的动作

class Blog {

    constructor(){
        this.content = '#content'
        this.router = {}
    }

    init(){
        this.dispatch()
        $(window).on('hashchange',()=>{ 
            this.dispatch()
        });
    }

    dispatch(){
        this.handle(window.location.hash)
    }

    addHandler(hash, func){
        if(Array.isArray(hash)){
            for(let item of hash){
                this.router[item] = func
            }
        }else{
            this.router[hash] = func
        }
    }

    handle(hash){
        if(this.routeMissing(hash)){
            this.handle404()
            return
        }
        this.router[hash].call(this)
    }

    routeMissing(hash){
        if(this.router[hash])
            return false
        else
            return true
    }

    handle404(){
        console.log('handler not found for this hash')
    }

    compile(templateSelector, data, element=this.content){
        let source   = $(templateSelector).html()
        let template = Handlebars.compile(source)
        $(element).html(template(data))
    }

}

this.router是个是核心,其实也参考了一点Rails的设计,通过一个对象去保存 路由=》动作 的关系, 并且把核心逻辑都封装在Blog这个类中。

相关文章

  • 自制简易前端MVC框架

    周末花了大概7小时写了一个简易的响应式blog,原意是练习css的,写着写着却去实现了一套前端路由并渲染的东西,这...

  • 通过样例来理解 MVC 模式

    参考: 自制前端框架之 MVC参考: MVC,MVP 和 MVVM 的图示 如何设计一个程序的结构,这是一门专门的...

  • 自制简易MVC

    最近稍微有点时间,并且我以前是做ng的,对ng的mvc一直想研究研究。MVC的基础知识我就不多说了,总结下来就是:...

  • 前端框架系列之(mvvm)

    简介 前面我们介绍过了mvc 前端框架系列之(mvc),mvp 前端框架系列之(mvp),MVP中我们说过随着业务...

  • angular2 路由策略 LocationStrategy

    随着前端技术的发展和迭代,前端MVC框架应运而生。利用目前主流的前端框架,如angular react vue等...

  • Java高并发秒杀Api-web 层

    章节目录 前端交互设计 Restful Spring MVC理论 整合配置MVC框架 bootstrap+jque...

  • java

    前端:bootstrap JavaScript jQuery后端:ORM技术,MVC模式,开源框架,spring

  • 万字长文聊缓存(下)- 应用级缓存

    深入解析SpringMVC核心原理:从手写简易版MVC框架开始(SmartMvc) : https://githu...

  • 万字长文聊缓存(上)- Http缓存

    深入解析SpringMVC核心原理:从手写简易版MVC框架开始(SmartMvc) : https://githu...

  • 玩转angularJS框架

    AngularJS AngularJS概述 介绍 简称:ng Angular是一个MVC框架 其他前端框架: Vu...

网友评论

      本文标题:自制简易前端MVC框架

      本文链接:https://www.haomeiwen.com/subject/ijqyottx.html