惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Jina AI
Jina AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
美团技术团队
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
有赞技术团队
有赞技术团队
GbyAI
GbyAI
宝玉的分享
宝玉的分享
腾讯CDC
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
月光博客
月光博客
MyScale Blog
MyScale Blog
Last Week in AI
Last Week in AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog

Secret Weblog

Becoming More Xee: A Modern XPath and XSLT Engine in Rust Looking for new challenges! Repeat Yourself, A Bit The Curious Case of Quentell The Humble For Loop in Rust The Humble For Loop in JavaScript Don Question Best Practices I Was a 1980s Teenage Programmer Part 5: Achieving Assembly I Was a 1980s Teenage Programmer Part 4: The Call of Assembly The Tooling Shift I Was a 1980s Teenage Programmer Part 3: MSX-2 JavaScript: when you need two ways to do it! Empowering Programming Languages Bloat and Retrofuturism Refreshing my Blog Again Random Rust Impressions Apilar: An Alife System I Was a 1980s Teenage Programmer Part 2: Olivetti M24 I Was a 1980s Teenage Programmer: the Alphatronic SolidJS fits my brain Is premature optimization the root of all evil? Framework Patterns: JavaScript edition Roll Your Own Frameworks Framework Patterns Secret Weblog Highlights Refactoring to Multiple Exit Points mstform: a form library for mobx-state-tree Seven Years: A Very Personal History of the Web
The Core of Obviel
Martijn Faassen · 2013-01-31 · via Secret Weblog

Start with jQuery

Obviel's core principle is pretty simple if you know jQuery.

With jQuery, you can select one or more elements like with a selector:

var el = $('#foo')

You get a list of wrapped DOM elements. You can do things with these:

el.text(); // get the text content

el.attr('foo'); // get an attribute

el.html('<p>Hello</p>'); // set the HTML content

Add the Obviel

Obviel expands jQuery with a render function:

el.render(obj); // render an object on el

Once you know this, you know the most important thing about Obviel.

Views

Now how does Obviel know how to render obj on el?

You need to tell Obviel first (typically when the JavaScript file is being loaded) by defining a view:

obviel.view({
  iface: 'animal',
  html: '<p>This is an animal</p>'
});

This view works for the iface animal. An iface is a simple marker on objects you want to render. So if you have this object:

var obj = {
  iface: 'animal'
};

We render obj on el:

el.render(obj);

Obviel knows that to render obj, it needs to use the view registered for animal above.

Once the view is found, Obviel renders the view on the element. In the case of the view above, it will insert <p>This is an animal</p> into el.

Obviel Template

Above we insert a snippet of HTML. Typically you want to insert something more specific to the object at hand. Let's expand the animal object a bit so it has some more information:

var obj = {
  iface: 'animal'
  name: 'lion',
  sound: 'growls'
};

Here's a view that will display a bit of information about such an animal with a name and sound property:

obviel.view({
  iface: 'animal',
  obvt: '<p>The {name} {sound}</p>'
});

When this view is rendered:

el.render(obj);

the <p>The lion growls</p> will be inserted into el.

The obvt bit above is an Obviel Template. Obviel Template lets you do variable substitution, repeat elements, bind events and has extensive i18n support.

Custom JavaScript

Finally, you may want to execute some custom JavaScript code when a view is rendered. You can do this by defining a render function on the view:

obviel.view({
   iface: 'animal',
   obvt: '<p class="animal">The {name} {sound}</p>',
   render: function() {
      $('.animal', this.el).click(function() {
        alert("You clicked on the animal: " + this.obj.name);
      });
   }
});

As you can see you can combine obvt (or html) with a custom render function. You can access the element on which the view is rendered with this.el and the object being rendered with this.obj.

In fact you can use Obviel Template's event handler support to clean up this code:

obviel.view({
   iface: 'animal',
   obvt: '<p class="animal" data-handler="click|animalClicked">The {name} {sound}</p>',
   animalClicked: function() {
     alert("You clicked on the animal: " + this.obj.name);
   }
});

More

This should get you started! But Obviel is extensively documented at http://obviel.org should you need more information.

Good luck!