官术网_书友最值得收藏!

Watching the filesystem

It is very convenient to update a web form from any computer. It may also be useful to update it purely from the filesystem so that a standard text editor can be used or we can have the blog post content entirely automated.

The watcher package provides the functionality needed for this feature. This package is authored by the Dart team and is typically of the Dart philosophy of being a small focused library. The DirectoryWatcher class takes a path and notifies via events if any files have added, removed, or modified.

The Blog class constructor configures the path that is going to be watched by the DirectoryWatcher. Let's take a look at the following screenshot:

  DirectoryWatcher _changeMonitor;

  Blog(this._pathPosts, this._pathImgs) {
    initBlog();
    _changeMonitor = new DirectoryWatcher(this._pathPosts);
    _changeMonitor.events.listen((watchEvent) => initBlog());
  }

The actual details of the change are not important for the blog, so the initBlog method will be called to reset the entire site.

In the content folder of this chapter's sample code, there is an unpublished blog post with the 7.txt filename. Start the main.dart blog server located in the bin folder and go to the index.html page. Open the file manager on your system and locate the 7.txt file. Move this file into the posts folder and refresh the web page. A new blog post should appear at the top of the page, as shown in the following screenshot:

Watching the filesystem

Return to the folder and move the 7.txt file out of the posts folder. Refresh the web browser again and the most recent post on Chicken Facts will have gone.

主站蜘蛛池模板: 宜兰县| 衡水市| 龙山县| 肇庆市| 常宁市| 定远县| 哈尔滨市| 高邑县| 彭山县| 武城县| 漾濞| 洪江市| 张家口市| 咸丰县| 崇信县| 新乡市| 榕江县| 介休市| 邛崃市| 根河市| 芜湖县| 峡江县| 彰化市| 黄龙县| 紫云| 昌图县| 卫辉市| 九江市| 莱西市| 巴林右旗| 灌南县| 朝阳县| 星座| 教育| 囊谦县| 梓潼县| 钦州市| 黄梅县| 丰镇市| 阿瓦提县| 同仁县|