Skip to content

Commit 26b8d99

Browse files
authored
Remove m.prop + m.withAttr (MithrilJS#2317)
* Remove `m.prop` + `m.withAttr` - For many uses, `m.withAttr` is *more* verbose than just directly using an event handler - If you're using it with a bound callback, you're literally wasting a single character in the human readable version (and you're *saving* them in the minified output). - It sometimes obscures your intent, if overused. - Functions are easier to compress than `m.withAttr`, resulting in slightly smaller bundles. - `m.withAttr` is overused anyways. - `m.prop` is basically useless without `m.withAttr`, and the API doesn't have the same benefits it had with 0.2.x. * Update changelog
1 parent 86c1682 commit 26b8d99

18 files changed

+28
-445
lines changed

docs/api.md

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -121,48 +121,6 @@ var querystring = m.buildQueryString({a: "1", b: "2"})
121121

122122
---
123123

124-
#### m.withAttr(attrName, callback) - [docs](withAttr.md)
125-
126-
```javascript
127-
var state = {
128-
value: "",
129-
setValue: function(v) {state.value = v}
130-
}
131-
132-
var Component = {
133-
view: function() {
134-
return m("input", {
135-
oninput: m.withAttr("value", state.setValue),
136-
value: state.value,
137-
})
138-
}
139-
}
140-
141-
m.mount(document.body, Component)
142-
```
143-
144-
---
145-
146-
#### m.prop(initial) - [docs](prop.md)
147-
148-
```javascript
149-
var Component = {
150-
oninit: function(vnode) {
151-
vnode.state.current = m.prop("")
152-
},
153-
view: function(vnode) {
154-
return m("input", {
155-
oninput: function(ev) { vnode.state.current.set(ev.target.value) },
156-
value: vnode.state.current.get(),
157-
})
158-
}
159-
}
160-
161-
m.mount(document.body, Component)
162-
```
163-
164-
---
165-
166124
#### m.trust(htmlString) - [docs](trust.md)
167125

168126
```javascript

docs/change-log.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- render: simplify component removal ([#2214](https://github.com/MithrilJS/mithril.js/pull/2214))
3434
- cast className using toString ([#2309](https://github.com/MithrilJS/mithril.js/pull/2309))
3535
- render: call attrs' hooks first, with express exception of `onbeforeupdate` to allow attrs to block components from even diffing ([#2297](https://github.com/MithrilJS/mithril.js/pull/2297))
36+
- API: `m.withAttr` removed. ([#2317](https://github.com/MithrilJS/mithril.js/pull/2317))
3637

3738
#### News
3839

@@ -47,7 +48,6 @@
4748
- API: add support for raw SVG in `m.trust()` string ([#2097](https://github.com/MithrilJS/mithril.js/pull/2097))
4849
- render/core: remove the DOM nodes recycling pool ([#2122](https://github.com/MithrilJS/mithril.js/pull/2122))
4950
- render/core: revamp the core diff engine, and introduce a longest-increasing-subsequence-based logic to minimize DOM operations when re-ordering keyed nodes.
50-
- API: Introduction of `m.prop()` ([#2268](https://github.com/MithrilJS/mithril.js/pull/2268))
5151
- docs: Emphasize Closure Components for stateful components, use them for all stateful component examples.
5252
- stream: Add `stream.lift` as a user-friendly alternative to `merge -> map` or `combine` [#1944](https://github.com/MithrilJS/mithril.js/issues/1944)
5353
- API: ES module bundles are now available for `mithril` and `mithril/stream` ([#2194](https://github.com/MithrilJS/mithril.js/pull/2194) [@porsager](https://github.com/porsager)).

docs/components.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,14 @@ var Login = {
365365
login: function() {/*...*/},
366366
view: function() {
367367
return m(".login", [
368-
m("input[type=text]", {oninput: m.withAttr("value", this.setUsername.bind(this)), value: this.username}),
369-
m("input[type=password]", {oninput: m.withAttr("value", this.setPassword.bind(this)), value: this.password}),
368+
m("input[type=text]", {
369+
oninput: function (e) { this.setUsername(e.target.value) },
370+
value: this.username,
371+
}),
372+
m("input[type=password]", {
373+
oninput: function (e) { this.setPassword(e.target.value) },
374+
value: this.password,
375+
}),
370376
m("button", {disabled: !this.canSubmit(), onclick: this.login}, "Login"),
371377
])
372378
}
@@ -411,11 +417,11 @@ var Login = {
411417
view: function() {
412418
return m(".login", [
413419
m("input[type=text]", {
414-
oninput: m.withAttr("value", Auth.setUsername),
420+
oninput: function (e) { Auth.setUsername(e.target.value) },
415421
value: Auth.username
416422
}),
417423
m("input[type=password]", {
418-
oninput: m.withAttr("value", Auth.setPassword),
424+
oninput: function (e) { Auth.setPassword(e.target.value) },
419425
value: Auth.password
420426
}),
421427
m("button", {

docs/nav-methods.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
- [m.jsonp](jsonp.md)
88
- [m.parseQueryString](parseQueryString.md)
99
- [m.buildQueryString](buildQueryString.md)
10-
- [m.withAttr](withAttr.md)
11-
- [m.prop](prop.md)
1210
- [m.trust](trust.md)
1311
- [m.fragment](fragment.md)
1412
- [m.redraw](redraw.md)

docs/prop.md

Lines changed: 0 additions & 152 deletions
This file was deleted.

docs/route.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ var Form = {
383383
},
384384
view: function() {
385385
return m("form", [
386-
m("input[placeholder='Search']", {oninput: m.withAttr("value", function(v) {state.term = v}), value: state.term}),
386+
m("input[placeholder='Search']", {
387+
oninput: function (e) { state.term = e.target.value },
388+
value: state.term
389+
}),
387390
m("button", {onclick: state.search}, "Search")
388391
])
389392
}
@@ -589,8 +592,14 @@ var Auth = {
589592
var Login = {
590593
view: function() {
591594
return m("form", [
592-
m("input[type=text]", {oninput: m.withAttr("value", Auth.setUsername), value: Auth.username}),
593-
m("input[type=password]", {oninput: m.withAttr("value", Auth.setPassword), value: Auth.password}),
595+
m("input[type=text]", {
596+
oninput: function (e) { Auth.setUsername(e.target.value) },
597+
value: Auth.username
598+
}),
599+
m("input[type=password]", {
600+
oninput: function (e) { Auth.setPassword(e.target.value) },
601+
value: Auth.password
602+
}),
594603
m("button[type=button]", {onclick: Auth.login}, "Login")
595604
])
596605
}

docs/simple-application.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,12 @@ module.exports = {
471471
}, [
472472
m("label.label", "First name"),
473473
m("input.input[type=text][placeholder=First name]", {
474-
oninput: m.withAttr("value", function(value) {User.current.firstName = value}),
474+
oninput: function (e) {User.current.firstName = e.target.value},
475475
value: User.current.firstName
476476
}),
477477
m("label.label", "Last name"),
478478
m("input.input[placeholder=Last name]", {
479-
oninput: m.withAttr("value", function(value) {User.current.lastName = value}),
479+
oninput: function (e) {User.current.lastName = e.target.value},
480480
value: User.current.lastName
481481
}),
482482
m("button.button[type=submit]", "Save"),

docs/stream.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,15 @@ In the example above, the `users` stream is populated with the response data whe
306306

307307
#### Bidirectional bindings
308308

309-
Streams can also be populated from other higher order functions, such as [`m.withAttr`](withAttr.md)
309+
Streams can also be populated from event callbacks and similar.
310310

311311
```javascript
312312
// a stream
313313
var user = stream("")
314314

315315
// a bi-directional binding to the stream
316316
m("input", {
317-
oninput: m.withAttr("value", user),
317+
oninput: function (e) { user(e.target.value) },
318318
value: user()
319319
})
320320
```

0 commit comments

Comments
 (0)