Skip to content

Commit 1aa7a95

Browse files
committed
v.5.0.0 contd
* array like collection has its own mod range * morph non-keyed nodes unconditionally so can update only when order changes * minor optimizations * separate jsx utils into utils-jsx.js
1 parent f7bb416 commit 1aa7a95

12 files changed

Lines changed: 2827 additions & 2904 deletions

README.md

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ view.model().get('items').map(item => (<ListItem data={item}/>))
216216

217217
**make sure** your custom component names **do not match default html element names!**
218218

219-
ModelView uses some heuristics in order to morph the real DOM as fast as posible and skip parts that haven't changed (or at least heuristics say so). However heuristics don't cover some edge cases (since these would require a deep diffing between real and virtual DOM, which beats the purpose of fast morphing). These cases are defined by implicitly dynamic parts which appear as static (they are implicitly dynamic as being parts of a larger dynamic element, whose static parts change as a whole, but are not marked as explicitly dynamic), while the explicitly dynamic parts are similar (see example below). These edge cases are however very easy to handle fully, by providing very simple hints to ModelView engine as to what to morph exactly and how.
219+
ModelView uses some speed heuristics in order to morph the real DOM as fast as posible and skip parts that haven't changed (or at least heuristics say so). However some heuristics don't cover some edge cases (since these would require a deep diffing between real and virtual DOM, which beats the purpose of fast morphing). Examples of such cases are defined by implicitly dynamic parts which appear as static (they are implicitly dynamic as being parts of a larger dynamic element, whose static parts change as a whole, but are not marked as explicitly dynamic), while the explicitly dynamic parts are similar (see example below). These edge cases are however very easy to handle fully, by providing very simple hints to ModelView engine as to what to morph exactly and how.
220220

221221
An example:
222222

@@ -258,56 +258,6 @@ someCondition ? view.unit(<ul><li>{text}</li><li>some static text</li></ul>) : v
258258

259259
In this case we mark the html nodes to be morphed completely as a single unit (ie `view.unit(..)`), instead of applying heuristics, so we have our expected result. Note that this solution is the more general, but might also be slightly slower in some cases.
260260

261-
ModelView is marking when nodes have actually changed. ModelView will apply changes if it sees some explicit changes in the new DOM. That means that if one simply changes the order of items (eg in an array) without actually changing the items or somehow mark them as changed, ModelView will not update the DOM to the new order. To do otherwise by default would be slow(er) and would slow down even in cases where this is not an issue. ModelView takes another route and lets user specify explicitly where more detailed (and slower) morphing should be applied.
262-
263-
An example:
264-
265-
```html
266-
<ul>{
267-
view.model().get('list').map(item => (<li>{item.text}</li>))
268-
}</ul>
269-
```
270-
```javascript
271-
view.model.get('list').sort((a,b) => 0.5 - Math.random()); // create a random order
272-
view.model().notify('list'); // it will not update the order if actual nodes haven't changed
273-
```
274-
275-
276-
However there are two very simple ways to remedy the situation:
277-
278-
279-
**1st way: use keyed nodes**
280-
281-
```html
282-
<ul>{
283-
view.keyed(view.model().get('list').map(item => (<li mv-id={item.id}>{item.text}</li>)))
284-
}</ul>
285-
```
286-
```javascript
287-
view.model.get('list').sort((a,b) => 0.5 - Math.random()); // create a random order
288-
view.model().notify('list'); // updates correctly
289-
```
290-
291-
In this case we use keyed nodes (`mv-id` special property) plus we mark `ul` as having keyed nodes via `view.keyed(..)` method. Having done this, another morphing routine handles changes, which will update to the correct order of items, even if no other changes are made to the individual items. In general it is good practice to use keyed nodes unless having specific reasons not to. In fact many popular frameworks work only with keyed nodes.
292-
293-
294-
**2nd way: use a Model.Collection (with or without keys)**
295-
296-
```javascript
297-
view.model().set('list', new ModelView.Model.Collection(itemsArray));
298-
```
299-
```html
300-
<ul>{
301-
view.model().get('list').mapTo(item => (<li>{item.text}</li>))
302-
}</ul>
303-
```
304-
```javascript
305-
view.model.get('list').sort((a,b) => 0.5 - Math.random()); // create a random order
306-
view.model().notify('list'); // updates correctly
307-
```
308-
309-
In this case `list` is a **Model.Collection**. Collection keeps note of what array operations are performed and mirrors those as DOM operations resulting in very efficient update code in general. So now order will update correctly even if no other changes are made to the individual items (see documentation for details).
310-
311261

312262
ModelView idea and implementation was based on some requirements. One of those is the ability of other actors to manipulate the DOM except ModelView itself. This was a desired feature. **ModelView does not claim exclusive manipulation of the DOM** (unlike frameworks like React or Vue or Inferno), other actors can manipulate the DOM and ModelView will still work (at least in most cases of interest). This is because ModelView relies on the **actual DOM** which is the **only reliable source of truth**. Additionally ModelView provides some necessary direct DOM-level manipulation methods (eg to handle some things even faster, like add/move/remove nodes directly) which can be used along with ModelView's general DOM morphing functionality.
313263

beeld.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tasks =[{}]
2323
### modelview.js, main framework ###
2424
./src/header.js
2525
./src/utils.js
26+
./src/utils-jsx.js
2627
./src/utils-simple.js
2728
./src/pb.js
2829
./src/types.js

build/modelview.bundle.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)