`
decentway
  • 浏览: 157876 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

YUI3:GET

阅读更多

Get 工具提供了一个,在页面加载完之后,附加脚本和css资源(包括跨域资源)到DOM上的机制。

Get工具两种常见的用例:

1.跨站数据检索:因为XMLHttplRequest(YUI IO Utility也是使用XMLHttpRequest)依靠一个严格的同源策略,所以,要通过XHR检索第三方数据需要一个服务器代理。(具体搜索一下XMLHttpRequest工作原理)。如果是能控制或者完全信任的跨域资源,可以不通过服务端代理而直接加载 script 数据从不同源的地方;而 script 文件,可能是典型 json 格式化的数据,在 load 的时候会立马执行。有一点至关重要,如果你加载的 script file 有恶毒的代码,没有安全的机制可以保证用户不受到恶毒代码的影响,因为浏览器会以完全的权限来执行这些代码。所以记住这一点:不要让用户接触到那些不能彻底信任的数据源

2.逐步加载方法:在富客户端应用中,基于用户动态加载需要的 js 文件和 css 文件变得非常有用, Get utility 提供了动态加载资源的功能。(注意:如果是加载 YUI 里面资源,可以使用 YUI loader 来加载, YUILoader 使用 Get Utility 来加载 YUI 组件,并且对解决 YUI 组件依赖问题)

 

 开始

配置一个Get Utility 事务(Configuring a Get Utility Transaction。请问如何翻译?)

使用提供给你的回调函数的参数.

Getting Started
Configuring a Get Utility Transaction
Making Use of Arguments Supplied to Your Callback
Using the Get Utility to Insert Script Nodes
Using the Get Utilty to Insert CSS Files
Using JSONP Web Services
How is the Get Utility Different From IO?
YUI on Mobile Devices

 

开始

<script src="http://yui.yahooapis.com/3.3.0/build/yui/yui-min.js"></script>;

 

 


 你可以取得一个脚本(Y.Get.script())and/or CSS(Y.Get.css())资源到页面。script和css方法都有一下参数:

1.URL(s):指定你需要加载到页面的URL(s), URL(s)可能是字符串或者字符串数组。

2.options:一个可选的配置对象,它包含了transaction(交互?)的信息;更多内容下面会讲到。

一个简单的文件请求可能如下:

YUI().YUI().use(function(Y) { var url = http://search.yahooapis.com/SiteExplorerService/V1/inlinkData?" + "appid=3wEDxLHV34HvAU2lMnI51S4Qra5m.baugqoSv4gcRllqqVZm3UrMDZWToMivf5BJ3Mom" + "&results=20&output=json&omit_inlinks=domain" + "&callback=YAHOO.example.SiteExplorer.callback" + "&query=http://developer.yahoo.com/yui/", obj = Y.Get.script(url, { onSuccess: function() { alert("file loaded"); }; }); // 'obj'将是一个只有一个成员的对象,tID, //它是transaction中的唯一身份标识符, //它具有以下格式:"q0","q1","q2"..."qn" will be an object with a });

 

 


 

配置Get Utility Transaction

通过第二个参数配置script和css方法,这个可选的参数包含一个对象,这个对象包含以下字段:

 

配置选项 目的作用
onSuccess (function) Callback method invoked by Get Utility when the requested file(s) have loaded successfully.
onFailure (function) Callback method invoked by Get Utility when an error is detected or abort is called.
onProgress (function) Callback method invoked by Get Utility after each node is inserted.
onTimeout (function) Callback method invoked by Get Utility if a timeout is detected.
onEnd (function) Callback method invoked by Get Utility when a transaction completes no matter how the transaction ended.
attributes (object) A hash of attributes to apply to the dynamically created nodes. You might use this to add media="print" to a css file, for example.
win (window) The window into which the loaded resource(s) will be inserted. Default: Y.config.win.
context (object) The execution context in which all callbacks will run. Default: the current YUI instance.
data (any) Data to pass as an argument to onSuccess or onFailure callbacks. Default: null.
autopurge (boolean) If set to true, script nodes will automatically be removed every 20 transactions (this number is globally configurable via theY.Get.PURGE_THRESH property); script nodes can be safely removed in most cases, as their contents (having executed) remain available. CSS nodes should not have this set to true as it will remove the CSS rules. Default: true for script nodes, false for CSS nodes.
timeout (int) Number of milliseconds to wait for a script to finish loading before timing out

使用提供给回调函数的参数

正如上面部分,你的回调函数(无论是onSuccess还是onFailure),能够使用提供给配置对象的data 成员(将如你已经在configuration中提供了一个这样的data成员—如上表格)。当然,并不是只有data这一成员可以使用,下面的成员也可以使用:

(说简单点就是,在onSuccess或者onFailure函数中,允许使用下面的参数:)

 

 

FIELD CONTENTS
tId (string) The unique identifier for this transaction; this string is available as the tIdmember of the object returned to you upon calling the script or css method.
data (any) The data field you passed in your configuration object when the script or css method was called. Default: null.
nodes (array) An array containing references to node(s) created in processing the transaction. These will be script nodes for JavaScript and link nodes for CSS.
win (window) The window object in which the nodes were created.
purge() (function) Calling the returned purge() method will immediately remove the created nodes. This is safe and prudent for JavaScript nodes, which do not need to persist. If CSS nodes are purged, the rules they contain are no longer available and the page will repaint accordingly.

 使用Get Utility插入脚本节点 var successHandler = function(o) { //o,包含了上表中所有描述的域。 o.purge(); //在执行之后立即移除script脚本节点。 Y.log(o.data); //传递给配置对象的data成员 } var objTransaction = Y.Get.script("http://json.org/json.js", { onSuccess: successHandler, data: { field1: value1, field2: value2 } })

 


 

 

当使用Y.Get.script()方法增加一个或者多个脚本文件到页面上时,Get Utility 按照以下步骤执行:

1.将一个脚本节点加载到请求脚本文件的目标窗口

2.将新脚本的src属性设置为指定的URL

3.如果transaction成功,脚本被加载执行。

4.脚本节点的load时间被激发

5.Get Utility检查是否有更多的URL,如果有,将返回第一步。

6.如果这个最后一个,Get Utility将唤醒onSuccess回调函数(如果指定了一个回调函数)

如下:

var handlerData = { //传递给success or failure句柄的数据。 //就像上面的。 }; var successHandler = function(oData) { //code to execute when all requested scripts have been //loaded; this code can make use of the contents of those //scripts, whether it's functional code or JSON data. }; var aURLs = [ "/url1.js", "/url2.js", "/url3.js" //等等。。。 ]; Y.Get.script(aURLs, { onSuccess: successHandler, data: handlerData });

 


 

 使用Get Utility 来插入CSS文件

当你使用使用Y.Get.css()方法来增加一个或者多个CSS文件到页面上时,Get Utility 遵循以上脚本步骤。在Firefox和Safari上不兼容。

使用JSONP Web Services

Get Utility 和IO的区别

 

分享到:
评论

相关推荐

    YUI Yahoo User Interface v3.5.0

    该版本的主要亮点包括:在Node.js中YUI可作为NPM模块正式引入了第2套皮肤“night”引入了App、Button、CSSButton、Handlebars、Pjax和TestConsole等组件重构了CSS网格,现在更加轻量级、灵活重构了Get,以支持更多的...

    ExpressYUI.zip

    Express YUI 是 YUI 应用的 Express 扩展。示例代码:var express = require('express')...app.get('/foo', expyui.expose(), function (req, res, next) {  res.render('foo'); }); 标签:Express

    express 框架介绍 安装 路由 动态路由 get传值 - avi.zip

    Express 框架是后台的 Node 框架,所以和 jQuery、zepto、yui、bootstrap 都不一个东西。 Express 在后台的受欢迎的程度类似前端的 jQuery,就是企业的事实上的标准

    asp.net 结合YUI 3.0小示例

    function onshengchange(e) { var sheng = Y.Node.get(‘#’+config.sheng).get(“value”); var speccode = Y.Node.get(‘#’+config.speciality); var years = Y.Node.get(‘#’+config.years); var timesli

    在layui中对table中的数据进行判断(0、1)转换为提示信息的方法

    需要把“1”改成中文汉字: 在 done方法里面进行数据渲染: layui.use('table', function()... method: 'post', //默认:get请求 cellMinWidth: 80, page: true, request: { pageName: 'pageNum', //页码的参数名

    Professional JavaScript Frameworks Prototype, YUI, Ext JS, Dojo and MooTools.pdf

    This book is for web developers who want to get straight into JavaScript and explore the tools and productivity gains offered by the frameworks. A working knowledge of HTML, CSS, and JavaScript are ...

    layui动态加载多表头的实例

    效果 动态生成header 返回的header数据:格式为数组,因为是二级表头,所以是二元数组 后台动态拼接数组,返回数组后把两个数组push到一个空数组中就可以组成一个动态的多级表头 ... type: get,

    gometa:golang 上的元数据

    gometa是一个基于go的元数据编程框架,很适合做企业应用软件开发。 gometa目标是让用户通过简单地配置即可完成增删改查功能,而专注于业务逻辑的编写和开发。 当前,gometa支持 1.数据库:MongoDB ...3.godec,(g

    解决使用layui的时候form表单中的select等不能渲染的问题

    今天小编就为大家分享一篇解决使用layui的时候form表单中的select等不能渲染的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    javascript YUI 读码日记之 YAHOO.util.Dom – Part.4

    var getXY = function() { // 判断是否是 IE if (document.documentElement.getBoundingClientRect) { // 注1 return function(el) { var box = el.getBoundingClientRect(); var rootNode = el....

    django-flatpage-widget:允许指定自定义窗口小部件以使用设置来编辑平面

    django-flatpage-widget ...该设置必须是两个元素的元组,其中第一个是使用django的get_callable解析为可调用对象的get_callable ,第二个是将作为关键字参数传递给callable的字典。 这适用于标准Widget类。

    Javascript YUI 读码日记之 YAHOO.util.Dom – Part.2 0

     el : Y.Dom.get(el); if (!el || !method) { return false; } // 确定返回的对象 var scope = (override) ? o : window; // 看起来是个 HTMLElement 或者不是 Array if (el.tagName

    Mojitomarkuptest.zip

     //mojit config (to use with ac.config.get() )  config: {  foo: 'bar'  },  //app level config (to use with ac.app)  appConfig: {  some: 'config param'  },  // We need to...

    JavaScript实战

    1.2 如何把JavaScript添加到页面 3 外部JavaScript文件 5 1.3 第一个JavaScript程序 7 1.4 把文本写到Web页面上 9 1.5 附件外部JavaScript文件 10 1.6 追踪错误 12 1.6.1 Firefox JavaScript控制台 13 1.6.2 显示...

    预加载css或javascript的js代码

    动态插入节点是最为简单也最为广泛的一种异步加载方式(例如yui的Get模块),然后使用动态插入节点方法加载的文件都会在加载后立即执行,javascript的执行一方面会占用浏览器js执行进程,另一方面也可能改变页面结构,...

    采用easyui tree编写简单角色权限代码的方法

    首先每个管理员得对应一个角色,而角色可以操作多个栏目,...action=RoleTree&Rid=' + raw.ID,\n method: 'get', animate: true, checkbox: true }); $('#Rid').val(raw.ID); 用了一个dialog弹出进行实现ashx中传入一

    The JavaScript Pocket Guide

    Almost every website today uses some form of JavaScript, whether it be used within a server-side programming technology like ASP.NET or a JavaScript library like jQuery or Mootools, or YUI or ...

    ExtAspNet v2.2.1 (2009-4-1) 值得一看

    注:ExtAspNet基于一些开源的程序ExtJS, HtmlAgilityPack, Nii.JSON, YUICompressor。 示例: http://extasp.net/ 开源: http://extaspnet.codeplex.com/ 博客: http://sanshi.cnblogs.com/ 邮箱: sanshi.ustc@gmail...

    ExtAspNet_v2.3.2_dll

    注:ExtAspNet基于一些开源的程序ExtJS, HtmlAgilityPack, Nii.JSON, YUICompressor。 示例: http://extasp.net/ 开源: http://extaspnet.codeplex.com/ 博客: http://sanshi.cnblogs.com/ 邮箱: sanshi.ustc@gmail...

    Layui 解决表格异步调用后台分页的问题

    参考Layui数据表格文档可以看到三种layui表格渲染方式,项目选用第一种,采用Layui的异步数据接口方式,渲染表格并分页。 按照官方写法,会报跨域错误,尝试几次,发现需要设置... method :'get', contentType: '

Global site tag (gtag.js) - Google Analytics