Use typescript to develop an online notepad that supports offline storage

Source: Internet
Author: User
Tags export class findone



First put the source Portal: Https://github.com/flowforever/yaryin.note



Notepad URL: http://yindoc.com, write your favorite file name at the back of the pound.






Recently, I studied nativescript,nativescript using typescript, so I studied TS by the way.



Have to mention Nativescript source learning, feel learned a lot of things, by the way also deducted from the above a dependency injection framework down with, the actual use of feeling very to force.



File Address: Https://github.com/flowforever/yaryin.note/blob/master/utils/yok.ts



In addition to a slight modification of the dependency, the other basically did not move.






TS gave me the first impression is crisp and clear, with Webstorm automatic compilation, the process of writing the code side of the compiler does not pass the hint very detailed.



First two pieces of code to paste:



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.ts


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
twenty one }
twenty two 
23 $ injector.register ('documentServices', Document); // sharp-eyed students will see this line of code, yes, here DocumentService is injected into the container, we do not need to require DocumentService class Write a pair of paths  








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‘);







Generally speaking:



TypeScript Development is a great force,



Nativescript's set of dependency injections is also very strong.






Use typescript to develop an online notepad that supports offline storage


Related Article

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.