使用 mootools 实现不显眼的 JavaScript ajax?

发布于 2024-12-23 07:51:30 字数 88 浏览 0 评论 0原文

MVC3 中提供了带有 jQ​​uery 的不显眼的 JavaScript。但是如何将不显眼的 Javascript ajax 与 mootools 一起使用呢?

Unobtrusive JavaScript with jQuery is available in MVC3.But how can I use the unobtrusive Javascript ajax with mootools?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-12-30 07:51:30

是的,这很简单。看看最近发布的 http://mootools.net/blog/2011 /12/20/mootools-behavior/,我认为它支持它。

我也在我的 Modal.BootStrap 中使用了这种方法(在 github 上查看源代码,链接在那里)它使用数据属性从 ajax 资源获取数据,虽然不太一样,但它确实是一个开始。

我只花了 10 分钟来做这个,这是一个好的开始:

http://jsfiddle.net/dimitar/zYLtQ/< /a>

(function() {

    var ajaxify = this.ajaxify = new Class({

        Implements: [Options,Events],

        options: {
            mask: "form[data-ajax=true]",
            props: {
                ajaxLoading: "data-ajax-loading",
                ajaxMode: "data-ajax-mode",
                ajaxUpdate: "data-ajax-update",
                ajaxSuccessEvent: "data-event-success"
            }  
        },

        initialize: function(options) {
            this.setOptions(options);
            this.elements = document.getElements(this.options.mask);
            this.attachEvents();
        },

        attachEvents: function() {
            this.elements.each(function(form) {
                var props = {};
                Object.each(this.options.props, function(value, key) {
                    props[key] = form.get(value) || "";
                });

                form.store("props", props);
                form.addEvent("submit", this.handleSubmit.bind(this));
            }, this);

        },

        handleSubmit: function(e) {
            e && e.stop && e.stop();
            var form = e.target, props = form.retrieve("props"), self = this;
            var updateTarget = document.getElement(props.ajaxUpdate);

            new Request({
                url: form.get("action"),
                data: form,
                onRequest: function() {
                    if (props.ajaxLoading) {
                        var loading = document.getElement(props.ajaxLoading);
                        if (loading && updateTarget) {
                            updateTarget.set("html", loading.get("html"));
                        }

                    }
                },
                onSuccess: function() {                             
                    if (!updateTarget)
                       return;

                    if(props.ajaxMode != 'append') {
                        updateTarget.set("html", this.response.text);
                    }
                    else {
                        updateTarget.adopt(new Element("div", { html: this.response.text }));
                    }     

                    if (props.ajaxSuccessEvent)
                        self.fireEvent(props.ajaxSuccessEvent, this.response);       
                }

            }).send();

        }

    });

})();


new ajaxify({
    onContactFormSuccess: function(responseObj) {
        console.log(responseObj.text);
        alert("we are done.");
    }
});

适用于以下 DOM:

<form action="/echo/html/" data-ajax="true"  data-ajax-loading="#loading" data-ajax-mode="replace" data-ajax-update="#update" data-event-success="contactFormSuccess" method="post">
    <input name="delay" value="4" type="hidden" />
    <input name="html" value="Thanks for your submission, this is the jsfiddle testing response" type="hidden" />
    <input name="name" placeholder="your name" />
    <button>submit</button>
</form>

<div id="update">The update will go here.</div>  
<div id="loading">loading...</div>        

您应该能够在此基础上进行构建。在重构时,我会将请求事件移至它们自己的方法中,并添加一些更多的校对等,但这很好。我不知道 mvc 的所有功能,但缺少一件事是表单验证事件。我还添加了一个自定义事件,完成后会触发,以便您的 ajaxifier 实例可以执行该表单特有的操作(请参阅 data-event-success="contactFormSuccess")

,它还可以使用默认请求选项,如果没有隐式指定,甚至创建什么请求对象 - Request、Request.HTML、Request.JSON 等。像 onRequest、spinners 等事件也是可行的...我认为你只需要按照 mvc 提供的选项进行操作即可构建它们以开始使用。

Confirm     data-ajax-confirm
HttpMethod  data-ajax-method
InsertionMode   data-ajax-mode *
LoadingElementDuration  data-ajax-loading-duration **
LoadingElementId    data-ajax-loading
OnBegin     data-ajax-begin
OnComplete  data-ajax-complete
OnFailure   data-ajax-failure
OnSuccess   data-ajax-success
UpdateTargetId  data-ajax-update
Url     data-ajax-url

yeah, this is trivial to do. have a look at the recently released http://mootools.net/blog/2011/12/20/mootools-behavior/, I think it supports it.

I have used this approach in my Modal.BootStrap (view source on github, link's there) as well whereby it uses data attributes to fetch data from an ajax resource, it's not quite the same but it certainly is a start.

I just spent 10 mins making this and it's a good start:

http://jsfiddle.net/dimitar/zYLtQ/

(function() {

    var ajaxify = this.ajaxify = new Class({

        Implements: [Options,Events],

        options: {
            mask: "form[data-ajax=true]",
            props: {
                ajaxLoading: "data-ajax-loading",
                ajaxMode: "data-ajax-mode",
                ajaxUpdate: "data-ajax-update",
                ajaxSuccessEvent: "data-event-success"
            }  
        },

        initialize: function(options) {
            this.setOptions(options);
            this.elements = document.getElements(this.options.mask);
            this.attachEvents();
        },

        attachEvents: function() {
            this.elements.each(function(form) {
                var props = {};
                Object.each(this.options.props, function(value, key) {
                    props[key] = form.get(value) || "";
                });

                form.store("props", props);
                form.addEvent("submit", this.handleSubmit.bind(this));
            }, this);

        },

        handleSubmit: function(e) {
            e && e.stop && e.stop();
            var form = e.target, props = form.retrieve("props"), self = this;
            var updateTarget = document.getElement(props.ajaxUpdate);

            new Request({
                url: form.get("action"),
                data: form,
                onRequest: function() {
                    if (props.ajaxLoading) {
                        var loading = document.getElement(props.ajaxLoading);
                        if (loading && updateTarget) {
                            updateTarget.set("html", loading.get("html"));
                        }

                    }
                },
                onSuccess: function() {                             
                    if (!updateTarget)
                       return;

                    if(props.ajaxMode != 'append') {
                        updateTarget.set("html", this.response.text);
                    }
                    else {
                        updateTarget.adopt(new Element("div", { html: this.response.text }));
                    }     

                    if (props.ajaxSuccessEvent)
                        self.fireEvent(props.ajaxSuccessEvent, this.response);       
                }

            }).send();

        }

    });

})();


new ajaxify({
    onContactFormSuccess: function(responseObj) {
        console.log(responseObj.text);
        alert("we are done.");
    }
});

works with a DOM of:

<form action="/echo/html/" data-ajax="true"  data-ajax-loading="#loading" data-ajax-mode="replace" data-ajax-update="#update" data-event-success="contactFormSuccess" method="post">
    <input name="delay" value="4" type="hidden" />
    <input name="html" value="Thanks for your submission, this is the jsfiddle testing response" type="hidden" />
    <input name="name" placeholder="your name" />
    <button>submit</button>
</form>

<div id="update">The update will go here.</div>  
<div id="loading">loading...</div>        

you should be able to build on that. on refactor i'd move the request events into their own methods and add some more proofing etc but it's fine. i don't know all mvc does but one thing that is missing is form validation events. i also added a custom event that is fired when done so your ajaxifier instance can do something particular to that form (see data-event-success="contactFormSuccess")

also, it can use default request options if not implicitly specified, even what request object to create - Request, Request.HTML, Request.JSON etc. Events like onRequest, spinners etc are also feasible... I think you just need to work your way through the options that mvc provides and build them to get started.

Confirm     data-ajax-confirm
HttpMethod  data-ajax-method
InsertionMode   data-ajax-mode *
LoadingElementDuration  data-ajax-loading-duration **
LoadingElementId    data-ajax-loading
OnBegin     data-ajax-begin
OnComplete  data-ajax-complete
OnFailure   data-ajax-failure
OnSuccess   data-ajax-success
UpdateTargetId  data-ajax-update
Url     data-ajax-url
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文