亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Backbone Network Verification
Backbone.LayoutManager
Backbone.ModelBinder
結(jié)論
Home CMS Tutorial WordPress Enhance trunk with extensions to improve experience

Enhance trunk with extensions to improve experience

Aug 30, 2023 pm 07:29 PM

Enhance trunk with extensions to improve experience

Backbone is becoming increasingly popular as a web application development framework. With this popularity came countless extensions and plugins to enhance the functionality of the framework and fill in the holes that others felt needed to be filled. Let’s take a look at some of the best options.


Backbone.Puppet

Developed by Derick Bailey, this extension is quite large and is my personal favorite. Instead of adding a feature or two to Backbone, Derick decided to fill all of the biggest holes that he felt existed. Here’s what he said about it in the GitHub project’s readme.

"Backbone.Marionette is a composite application library for Backbone.js designed to simplify building large JavaScript applications. It is a collection of common design and implementation patterns discovered in applications I (Derick Bailey) have built using Backbone, and includes Various pieces inspired by composite application architecture, such as Microsoft's "Prism" framework."

Let’s take a closer look at what Marionette has to offer us:

  • Application: This is the central object through which everything in the application communicates. It provides a quick and easy way to set up the main view of the application, a central event hub through which every module in the application can communicate so that they are not dependent on each other, and provides Initializer for granular control over how your application starts.
  • Modules: A way to encapsulate module code and name these modules on a central application object.
  • View: The new view class to extend provides native methods for cleanup so you don't end up with memory leaks. It also contains rendering templates; for simple views, just specify the template and model and it will take care of the rest.
  • Collections/Composite Views: This is where the "Composite Application Library" comes into play. These two views allow you to easily create views that can handle the process of rendering all views in a collection, even nested hierarchies of collections and models, with very little effort.
  • Regions and Layouts: A region is an object that handles all the work of rendering, unrendering, and closing views at a specific location in the DOM. A layout is just a normal view with built-in areas for working with subviews.
  • AppRouter: A new type of router that can use a controller to handle the workload so that the router only contains the configuration of the route.
  • Events: Marionette extends from the Wreqr project to make Backbone's events more powerful and can be used to create large-scale event-based applications.

I've only scratched the surface of what Marionette can do. I definitely recommend going to GitHub and reading their documentation on the wiki.

Additionally, Andrew Burgess covers Marionette in his Tuts Premium Advanced Backbone Patterns and Techniques course.


Backbone Network Verification

Backbone.Validation is designed to fill a small subset of problems: namely model validation. Backbone has multiple validation extensions, but this one seems to have gained the most respect from the community.

In fact, instead of writing a validate method for the model, you can create a validation property for the model, which is an object specifying each property you wish to validate and Rules that validate each attribute of the list. You can also specify error messages for each property and pass in a custom function to validate individual properties. You can see an example below, modified from one of the examples on their website.

var SomeModel = Backbone.Model.extend({
    validation: {
        name: {
            required: true
        },
        'address.street': {
            required: true
        },
        'address.zip': {
            length: 4
        },
        age: {
            range: [1, 80]
        },
        email: {
            pattern: 'email',
            // supply your own error message
            msg: "Please enter a valid email address"
        },
        // custom validation function for `someAttribute`
        someAttribute: function(value) {
            if(value !== 'somevalue') {
                return 'Error message';
            }
        }
    }
});

There are countless built-in validators and schemas you can check, and there's even a way to extend the list with your own global validator. This Backbone plugin doesn’t make validation fun, but it does remove any excuse for not adding validation. Please visit their website for more examples and more in-depth instructions on how to use this wonderful tool.


Backbone.LayoutManager

Backbone.LayoutManager is to make Backbone’s views better. Like Backbone.Marionette, it introduces cleanup code to prevent memory leaks, handles all boilerplate, and leaves you with only configuration and application-specific code. Unlike Marionette, LayoutManager focuses specifically on views.

Because LayoutManager only focuses on views, it can do more for views than Marionette. For example, LayoutManager is able to perform asynchronous rendering when you want to dynamically load a template from an external file.

LayoutManager can also handle subviews, albeit in a very different way than Marionette. But either way, it's mostly configuration, which makes things very simple and eliminates 90% of the work you'd need to do if you were trying to implement it all yourself. Take a look at the following simple example of adding three subviews to a view:

Backbone.Layout.extend({
    views: {
      "header": new HeaderView(),
      "section": new ContentView(),
      "footer": new FooterView()
    }
});

像往常一樣,請務(wù)必參閱 GitHub 頁面和文檔以了解更多信息。


Backbone.ModelBinder

Backbone.ModelBinder 在模型中的數(shù)據(jù)和視圖顯示的標(biāo)記之間創(chuàng)建鏈接。您已經(jīng)可以通過綁定到模型上的更改事件并再次渲染視圖來完成此操作,但 ModelBinder 更高效且更易于使用。

看一下下面的代碼:

var MyView = Backbone.View.extend({
    template: _.template(myTemplate),

    initialize: function() {
        // Old Backbone.js way
        this.model.on('change', this.render, this);
        // or the new Backbone 0.9.10+ way
        this.listenTo(this.model, 'change', this.render);
    },

    render: function() {
        this.$el.html(this.template(this.model.toJSON()));
    }
});

這種方法的問題在于,每當(dāng)更改單個(gè)屬性時(shí),我們都需要重新渲染整個(gè)視圖。此外,在每次渲染時(shí),我們都需要將模型轉(zhuǎn)換為 JSON。最后,如果希望綁定是雙向的(當(dāng)模型更新時(shí),DOM 也會更新,反之亦然),那么我們需要向視圖添加更多邏輯。

此示例使用 Underscore 的 template 函數(shù)。讓我們假設(shè)我們傳遞給它的模板如下所示:

<form action="...">
    <label for="firstName">First Name</label>
    <input type="text" id="firstName" name="firstName" value="<%= firstName %>">

    <label for="lastName">Last Name</label>
    <input type="text" id="lastName" name="lastName" value="<%= lastName %>">
</form>

使用標(biāo)簽 <%=%> 使 template 函數(shù)將這些區(qū)域替換為我們從模型發(fā)送的 JSON 中存在的 firstNamelastName 屬性。我們假設(shè)該模型也具有這兩個(gè)屬性。

使用 ModelBinder,我們可以刪除這些模板標(biāo)簽并以普通 HTML 形式發(fā)送。 ModelBinder 將查看 input 標(biāo)記上的 name 屬性的值,并自動將該屬性的模型值分配給該標(biāo)記的 value 屬性。例如,第一個(gè) inputname 設(shè)置為“firstName”。當(dāng)我們使用 ModelBinder 時(shí),它會看到這一點(diǎn),然后將 inputvalue 設(shè)置為模型的 firstName 屬性。

下面,您將看到我們之前的示例在切換到使用 ModelBinder 后的外觀。另外,要意識到綁定是雙向的,因此如果 input 標(biāo)簽更新,模型將自動為我們更新。

HTML 模板:

<form action="...">
    <label for="firstName">First Name</label>
    <input type="text" id="firstName" name="firstName">

    <label for="lastName">Last Name</label>
    <input type="text" id="lastName" name="lastName">
</form>

JavaScript 視圖:

var MyView = Backbone.View.extend({
    template: myTemplate,

    initialize: function() {
        // No more binding in here
    },

    render: function() {
        // Throw the HTML right in
        this.$el.html(this.template);
        // Use ModelBinder to bind the model and view
        modelBinder.bind(this.model, this.el);
    }
});

現(xiàn)在我們有了干凈的 HTML 模板,我們只需要一行代碼就可以使用 modelBinder.bind 將視圖的 HTML 和模型綁定在一起。 modelBinder.bind 采用兩個(gè)必需參數(shù)和一個(gè)可選參數(shù)。第一個(gè)參數(shù)是將綁定到 DOM 的數(shù)據(jù)的模型。第二個(gè)是將遞歸遍歷的 DOM 節(jié)點(diǎn),搜索要進(jìn)行的綁定。最后一個(gè)參數(shù)是 binding 對象,如果您不喜歡默認(rèn)用法,它指定如何完成綁定的高級規(guī)則。

ModelBinder 不僅僅可以用于 input 標(biāo)簽。它適用于任何元素。如果元素是某種類型的表單輸入,例如 input、selecttextarea,它將相應(yīng)地更新這些元素的值。如果您綁定到一個(gè)元素,例如 divspan,它會將模型的數(shù)據(jù)放置在該元素的開始和結(jié)束標(biāo)記之間(例如 <span name="firstName">[數(shù)據(jù)在此處] <span></span></span>)。

Backbone.ModelBinder 背后的功能比我在這里演示的要強(qiáng)大得多,因此請務(wù)必閱讀 GitHub 存儲庫上的文檔以了解所有相關(guān)信息。


結(jié)論

事情就這樣結(jié)束了。我只介紹了少數(shù)擴(kuò)展和插件,但這些是我認(rèn)為最有用的。

Backbone 格局在不斷變化。如果您想查看各種可用 Backbone 擴(kuò)展的完整列表,請?jiān)L問此 wiki 頁面,Backbone 團(tuán)隊(duì)會定期更新該頁面。

The above is the detailed content of Enhance trunk with extensions to improve experience. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How to diagnose high CPU usage caused by WordPress How to diagnose high CPU usage caused by WordPress Jul 06, 2025 am 12:08 AM

The main reasons why WordPress causes the surge in server CPU usage include plug-in problems, inefficient database query, poor quality of theme code, or surge in traffic. 1. First, confirm whether it is a high load caused by WordPress through top, htop or control panel tools; 2. Enter troubleshooting mode to gradually enable plug-ins to troubleshoot performance bottlenecks, use QueryMonitor to analyze the plug-in execution and delete or replace inefficient plug-ins; 3. Install cache plug-ins, clean up redundant data, analyze slow query logs to optimize the database; 4. Check whether the topic has problems such as overloading content, complex queries, or lack of caching mechanisms. It is recommended to use standard topic tests to compare and optimize the code logic. Follow the above steps to check and solve the location and solve the problem one by one.

How to minify JavaScript files in WordPress How to minify JavaScript files in WordPress Jul 07, 2025 am 01:11 AM

Miniving JavaScript files can improve WordPress website loading speed by removing blanks, comments, and useless code. 1. Use cache plug-ins that support merge compression, such as W3TotalCache, enable and select compression mode in the "Minify" option; 2. Use a dedicated compression plug-in such as FastVelocityMinify to provide more granular control; 3. Manually compress JS files and upload them through FTP, suitable for users familiar with development tools. Note that some themes or plug-in scripts may conflict with the compression function, and you need to thoroughly test the website functions after activation.

How to optimize WordPress without plugins How to optimize WordPress without plugins Jul 05, 2025 am 12:01 AM

Methods to optimize WordPress sites that do not rely on plug-ins include: 1. Use lightweight themes, such as Astra or GeneratePress, to avoid pile-up themes; 2. Manually compress and merge CSS and JS files to reduce HTTP requests; 3. Optimize images before uploading, use WebP format and control file size; 4. Configure.htaccess to enable browser cache, and connect to CDN to improve static resource loading speed; 5. Limit article revisions and regularly clean database redundant data.

How to use the Plugin Check plugin How to use the Plugin Check plugin Jul 04, 2025 am 01:02 AM

PluginCheck is a tool that helps WordPress users quickly check plug-in compatibility and performance. It is mainly used to identify whether the currently installed plug-in has problems such as incompatible with the latest version of WordPress, security vulnerabilities, etc. 1. How to start the check? After installation and activation, click the "RunaScan" button in the background to automatically scan all plug-ins; 2. The report contains the plug-in name, detection type, problem description and solution suggestions, which facilitates priority handling of serious problems; 3. It is recommended to run inspections before updating WordPress, when website abnormalities are abnormal, or regularly run to discover hidden dangers in advance and avoid major problems in the future.

How to use the Transients API for caching How to use the Transients API for caching Jul 05, 2025 am 12:05 AM

TransientsAPI is a built-in tool in WordPress for temporarily storing automatic expiration data. Its core functions are set_transient, get_transient and delete_transient. Compared with OptionsAPI, transients supports setting time of survival (TTL), which is suitable for scenarios such as cache API request results and complex computing data. When using it, you need to pay attention to the uniqueness of key naming and namespace, cache "lazy deletion" mechanism, and the issue that may not last in the object cache environment. Typical application scenarios include reducing external request frequency, controlling code execution rhythm, and improving page loading performance.

How to prevent comment spam programmatically How to prevent comment spam programmatically Jul 08, 2025 am 12:04 AM

The most effective way to prevent comment spam is to automatically identify and intercept it through programmatic means. 1. Use verification code mechanisms (such as Googler CAPTCHA or hCaptcha) to effectively distinguish between humans and robots, especially suitable for public websites; 2. Set hidden fields (Honeypot technology), and use robots to automatically fill in features to identify spam comments without affecting user experience; 3. Check the blacklist of comment content keywords, filter spam information through sensitive word matching, and pay attention to avoid misjudgment; 4. Judge the frequency and source IP of comments, limit the number of submissions per unit time and establish a blacklist; 5. Use third-party anti-spam services (such as Akismet, Cloudflare) to improve identification accuracy. Can be based on the website

How to enqueue assets for a Gutenberg block How to enqueue assets for a Gutenberg block Jul 09, 2025 am 12:14 AM

When developing Gutenberg blocks, the correct method of enqueue assets includes: 1. Use register_block_type to specify the paths of editor_script, editor_style and style; 2. Register resources through wp_register_script and wp_register_style in functions.php or plug-in, and set the correct dependencies and versions; 3. Configure the build tool to output the appropriate module format and ensure that the path is consistent; 4. Control the loading logic of the front-end style through add_theme_support or enqueue_block_assets to ensure that the loading logic of the front-end style is ensured.

How to add custom fields to users How to add custom fields to users Jul 06, 2025 am 12:18 AM

To add custom user fields, you need to select the extension method according to the platform and pay attention to data verification and permission control. Common practices include: 1. Use additional tables or key-value pairs of the database to store information; 2. Add input boxes to the front end and integrate with the back end; 3. Constrain format checks and access permissions for sensitive data; 4. Update interfaces and templates to support new field display and editing, while taking into account mobile adaptation and user experience.

See all articles