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

wxapp-observers

v1.0.4

Published

微信小程序一个兼容监听函数的工具

Downloads

11

Readme

wxapp-observers

wxapp-observers是一个可以监听data属性变化的工具,可以实现数据监听计算的方法。详情可以查询微信小程序2.6.1版本的组件数据监听器。里面包含了版本判断,所以是属于一个兼容微信新增监听函数的一个工具,并且在page页也可以使用。

wxapp-observers的第二个功能是可以规范page对象函数的分类,方法放置methods中,事件放置events中,生命周期放置lifetimes中,这样可以使代码有较强的整洁性和维护性。

安装&引用

npm i -D wxapp-observers

也可以在github上dist/observers.js下载使用


引用方式很简单

import 'observer'

使用说明

点击查看代码片段


我们可以在生命周期函数中设置lifetimes

Page({
	lifetimes: {
		onLoad() {
			console.log('================这里是page================')
		},
	},
})

也可以在把方法放置methods

Page({
	lifetimes: {
		onLoad() {
			this.method();
			console.log('================这里是page================')
		},
	},
	methods: {
		method(){
			// 注意methods里面的方法或外面的方法不能同名,也不能设置关键字"lifetimes, methods, events"
			console.info('方法可以写在这里')
		}
	}
})

还可以把事件放入events

// index.wxml
<view bindtap='event' class="intro">测试请看控制台打印</view>

// index.js
Page({
	events: {
		event(){
			console.log('触发了tap事件')
		}
	}
})

这样我们可以更好的代码整洁性和维护性


监听函数的使用

// 我们一切数据的监听行为都在this.$setData中使用
Page({
	data: {
		autor: 'fatwill',

		obj: {
			a: "对象"
		},

		numA: 1,

		numB: 2,
	},
	lifetimes: {
		onLoad() {
			this.$setData({
				autor: 'change 1'
			})

			this.$setData({
				'obj.a': 'change 2',
			})

			this.$setData({
				'a.b.c.d.e.f.g': '不存在的对象'
			})

			this.$setData({
				'numA': 10
			})

			this.method();

			console.log("================这里是page================")
		},
	},

	observers: {
		'autor' (value) {
			console.log('触发了autor的监听, 改变值为' + value)
		},
		'obj.a' (value) {
			console.log('触发了obj的监听,改变值为' + value)
		},
		'a.b.c.d.e.f.g' (value) {
			console.log('触发了a.b.c.d.e.f.g的监听,改变值为' + value)
		},
		'numA, numB' (numA, numB) {
			let sum = numA + numB;
			console.log(numA, numB)
			console.log('也可以作为一个computed触发,numA + numB = ' + sum);
			this.$setData({
				numC: sum
			})
		},
		'numC' (value) {
			console.log("触发了numC的回调。numA 和 numB的总和为" + value)
		}
	},

	methods: {
		method(){
			console.info('方法可以写在这里')
		}
	}
})

详细的使用方式我在这边不多细讲,因为这套监听方法是兼容微信小程序2.6.1版本的组件数据监听器的使用方法做的兼容,这套监听工具的好处就在于在微信版本合适的范围内用微信的数据监听组件,而在原生不兼容组件使用这一套框架所带来的数据监听,而且是零侵入式的引入。

注意事项

  • 我们的一切监听行为都以this.$setData函数去执行,当版本在2.6.1时则会直接引用原生函数this.setData的回调,另一方面,如果没有使用this.$setData也不会对原有项目造成任何影响
  • 除了使用lifetimesmethodsevents实现绑定相关函数(也不能在对象里面使用其他类型),不能在其他地方进行绑定或使用,否则可能会出现不可预知的错误。

bug&tip

  • 组件中的properties的改变无法在observers对象中监听,为了兼容的话建议使用properties中的observer
  • 其他欢迎在issue中提出