Coder Social home page Coder Social logo

Comments (2)

bytethinker avatar bytethinker commented on June 10, 2024 1

OK,thank you for writing up such a detailed reply!
Keep up the great idea!!

from cell.

gliechtenstein avatar gliechtenstein commented on June 10, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.