使用TypeScript開發一個在線記事本,支持離線存儲

先貼上源碼傳送門: https://github.com/flowforever/yaryin.notenode

記事本網址: http://yindoc.com , 井號後面寫你喜歡的文件名便可。git

 

最近在研究NativeScript,NativeScript使用TypeScript,因而就順便研究了ts。github

不得不提到NativeScript源碼學習,感受學習到了很多東西,順便也從上面扣了一個依賴注入的框架下來用,實際使用感受很是給力。express

文件地址: https://github.com/flowforever/yaryin.note/blob/master/utils/yok.tsapi

除了稍微修改一下依賴,其餘基本沒動。框架

 

ts 給個人第一印象就是清爽分明,配合WebStorm逆天的自動編譯,寫的過程當中代碼哪邊編譯不經過提示很是詳細。異步

先貼兩段代碼:mongoose

serviceBase.ts函數

/**
 * Created by trump on 15/4/23.
 */
///<reference path="../_references.d.ts"/>
/// <reference path="./_references.d.ts"/>
import db = require('../db/db');
import Future = require("fibers/future");
import Fiber = require('fibers');

export class ServiceBase {

    constructor(table) {
        this.table = table; // 這個table就是mongoose的Model
        this.$table = Future.wrap(table); //配合node fibber 解決異步callback hell 太給力了
    }

    table;
    $table;

    getAll() : IFuture<any> {
        return this.$table.findFuture.bind(this.table)({});
    }

    add(model): IFuture<any> {
        return this.$table.createFuture.bind(this.table)(model);
    }

    findById(id: string): IFuture<any> {
        return this.$table.findOneFuture.bind(this.table)({
            _id: id
        });
    }

    find(query:any): IFuture<any>{
        return this.$table.findFuture.bind(this.table)(query);
    }

    findOne(query:any): IFuture<any>{
        return this.$table.findOneFuture.bind(this.table)(query);
    }
}

 

documentServices.tspost

 1 /// <reference path="./_references.d.ts"/>
 2 import db = require('../db/db');
 3 
 4 import Future = require("fibers/future");
 5 import Fiber = require('fibers');
 6 import sb = require('./servicesBase');
 7 
 8 export class Document extends sb.ServiceBase {
 9 
10     constructor($db) {
11         super( $db.Document );
12         this.db = $db;
13     }
14 
15     db;
16 
17     getList() : IFuture<any> {
18         return this.getAll();
19     }
20 
21 }
22 
23 $injector.register('documentServices', Document); // 眼尖的同窗會看到這行代碼,沒錯這邊將DocumentService注入到容器裏面,在接下來的controller中咱們就不須要require DocumentService 這個類寫一對的路徑了

 

 

controller/api.ts

///<reference path="../_references.d.ts"/>
import express = require('express');

import services = require('../../services/documentServices');

class Controller {

    constructor($documentServices) {
        this.services = $documentServices; // 咱們這邊只須要在構造函數裏面指定好依賴的名稱,yok框架就幫咱們作好一切了
    }

    services;// = <services.Document>$injector.resolve('documentServices');

    'get/:name'(req:express.Request, res:express.Response) {
        (()=> {
            var doc = this.services.findOne({
                name: req.params.name
            }).wait();
            res.send(doc||{});
        }).future()();
    }

    '[post]edit'(req:express.Request, res:express.Response) {
        (()=> {
            var saved = null;
            if(!req.body._id) {
                saved = this.services.add({
                    name: req.body.name
                    , content: req.body.content
                }).wait();
                res.send(saved);
            } else {
                saved = this.services.findById(req.body._id).wait();
                saved.content = req.body.content;
                saved.name = req.body.name;
                saved.save(function(){
                    res.send(saved);
                });
            }
        }).future()()
    }

    rename(req:express.Request, res:express.Response) {

    }

    remove(req, res) {

    }

}

$injector.register('apiHomeController', Controller);

module.exports = $injector.resolve('apiHomeController');

 

整體來講:

TypeScript 開發很給力,

NativeScript的那套依賴注入也很給力。

相關文章
相關標籤/搜索