Comments (2)
OK,thank you for writing up such a detailed reply!
Keep up the great idea!!
from cell.
Thanks for asking. This is probably the only gotcha when it comes to how contexts get resolved on Cell, so I'm happy to answer.
There's one thing to remember about how _
variables trigger the $update()
function.
They need to be triggered inside a function that's directly attached on the element context. Let me explain what this means.
When you do:
$init: function(){
this._items = [{tag: "aaaa"}];
}
That will trigger an $update()
automatically.
However if you do
$init: function(){
var self = this;
setTimeout(function(){
self._items = [{tag: "aaaa"}];
}, 2000)
}
That won't trigger $update()
because the mutation is happening inside the setTiemout
's anonymous callback function (and this callback function doesn't exist on the element's context, it's just a parameter passed to setTimeout
).
So to make this work, we need to make sure the mutation logic happens directly inside a function that's attached to the element context, like this:
$init: function(){
var self = this;
setTimeout(function() {
self._refresh([{tag: 'aaaa'}]);
},2000);
},
_refresh: function(items){
this._items = items;
},
Note how we've created a separate _refresh
function, and calling it from setTmeout
. This time it works because the mutation this._items = items
is happening inside the _refresh()
function, which is directly tied to the element's context.
The End result:
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://www.celljs.org/cell.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet">
<script>
var GithubApp = {
$cell: true,
_items: [{tag:'bbbb'}],
$init: function(){
var self = this;
window.setTimeout(function()
{
self._refresh([{tag: 'aaaa'}]);
},2000);
},
_refresh: function(items){
this._items = items;
},
_template: function(item){
return {
$components: [{
$text: item.tag
}]
};
},
$update: function(){
this.$components = this._items.map(this._template);
}
}
</script>
</html>
Hope this makes sense, please let me know if you have further questions!
from cell.
Related Issues (20)
- Define a variable's attribute using another attribute of itself HOT 1
- Devise a way to express CSS in cell HOT 1
- $update not fired when components have a $init defined HOT 2
- Cell Native? HOT 5
- Safari Issues HOT 2
- Assigning $text from component won't update DOM. HOT 4
- $update not being triggered/called HOT 1
- ES6 functions HOT 1
- Distinguish between SVG <text> elements and normal text HOT 1
- Scoped CSS HOT 1
- Break out createElementNS(Hardcoded) into Monkey-Patchable Function - GeneReader ? HOT 1
- Can you share an example with react components?
- Suggestion : Limiting complexity of inter-cell dependency HOT 1
- js-framework-benchmark
- Select multiple and attribute order
- Handy way to get cell components
- Injecting CellJs via external file HOT 1
- Using web components with Cell
- mutation of a cell HOT 1
- Development Moved to https://github.com/jasonelle/jasonelle HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cell.