npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ycloud-lifecycle

v1.0.4

Published

- 为了规划代码 - 为了代码更好的可读性 - 为了提高代码的可维护性

Downloads

12

Readme

为什么会有生命周期

  • 为了规划代码
  • 为了代码更好的可读性
  • 为了提高代码的可维护性

通常开发一个MVVM的前端页面会涉及到 数据、初始化函数、计算属性、属性监听、交互事件、和后台进行ajax数据交换之类的代码。 生命周期就好比一个我们定义了一个抽屉,我们把代码进行分类后放到何时的抽屉之中,这样我们在开发和维护的时候大家的代码就都是一致的、统一的、可预期的。 从做产品长期维护的角度来看,会大大提升代码的可维护性。

关于生命周期钩子

  • init:通常用于初始化页面,在页面初始化之前可以做任意的事情,比如在定义data时需要先从url里拿一些参数,或者在init里处理通用的页面行为分析统计等等。
  • data:定义本地属性data的方法,类似与vue生命周期里的data
  • computed: 定义计算属性的方法
  • watch: 定义属性监听(ko.subscribe)方法
  • methods: 定义页面事件监听方法
  • beforeMounted: 一般用于将vm挂载到dom上的ko.applybindings
  • mounted: 挂载之后, 通常页面业务处理的逻辑代码从这里开始

如何使用

import {BaseView} from 'ycloud-lifecycle'
import ko from 'knockout'
class View extends BaseView {
  beforeInit () {
    console.log('beforeInit')
  }
  // 初始化
  init () {
    console.log('page init')
  }
  afterInit () {
    console.log('afterInit')
  }
  // 普通ko属性
  data (params) {
    this.title = ko.observable(params.title)
  }
  // 计算属性
  computed () {
    this.titleComputed = ko.computed(() => {
      return this.title() + 'i am a boy'
    })
  }
  // 属性监听
  watch () {
    this.title.subscribe((val) => {
      console.log('watch title:' + val)
    })
  }
  //事件 方法 
  methods () {
    this.click = (data, evt) => {
      console.log(data)
      console.log(evt)
    }
    this.method1 = function (val, test) {
      console.log(val + test)
    }
  }
  beforeMounted () {
    console.log('beforeMounted')
    ko.applyBindings(this, document.getElementById('app'))
  }
  mounted () {
    this.$on('testeventemmit', this.method1)
    console.log('mounted' + this.title())
  }
  afterMounted () {
    console.log('mounted' + this.title())
    this.$emit('testeventemmit', 'i am emit', ' heyboy')
    this.$off('testeventemmit', this.method1)
    setTimeout(() => {
      this.$emit('testeventemmit', 'i am emit', ' heyboy')
    }, 2000)
  }
}
new View({title: '新title:'})

update log:

  • 2018-01-02 添加eventemitter2 增加$on,$emit,$off