- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我有一个 durandal 应用程序,可以通过 ajax 发送数据,但我不知道如何实现加载指示器,以下是代码:
这是加载数据的 View loadinbox.html
<div class="modal-content messageBox">
<div class="modal-header">
<h3>LOGIN</h3>
</div>
<div class="modal-body">
<h3>Entre com suas credenciais.</h3>
<form data-bind="submit: ok">
<input type="text" data-bind="value: login" placeholder="CPF" class="form-control autofocus" />
<input type="text" data-bind="value: senha" placeholder="Senha" class="form-control autofocus" />
</form>
</div>
<div data-bind="if: $parent.loading">
<img src="img/loading.gif"/>
</div>
<div class="modal-footer">
<button class="btn btn-success" data-bind="click: ok, active: $parent.loading">Login</button>
</div>
</div>
这是加载数据的模型登录框.js
define(function (require) {
var dialog = require('plugins/dialog');
var loading = ko.observable();
var loginBox = function(){
this.login = '';
this.senha = '';
this.loading = false;
};
loginBox.prototype.ok = function () {
this.loading =true;
$.ajax({
type: "post",
data: { "LoginForm[cpf]" : this.login, "LoginForm[password]" : this.senha , 'ajax':'login-form' },
url: localStorage['baseurl']+localStorage['router']+'site/login',
success: function (data){
console.log(data);
},
error: function (request, status, error){
console.log(request.status);
console.log(status);
console.log(error);
},
complete: function (data) {
alert('hqweuiqhioehqio');
this.loading =false;
}
});
};
loginBox.show = function() {
return dialog.show(new loginBox());
};
return loginBox;
});
最佳答案
从表面上看,您的方法是合理的,但是您对 Durandal 中的模块的方法有点困惑。例如,您已声明 loading
两次,一次作为标量,一次作为可观察量。
所以,让我们创建一个实例模块(这意味着我们将返回一个构造函数):
loginBox.html( View )
<div class="modal-content messageBox">
<div class="modal-header">
<h3>LOGIN</h3>
</div>
<div class="modal-body">
<h3>Entre com suas credenciais.</h3>
<form data-bind="submit: ok">
<input type="text" data-bind="value: login" placeholder="CPF" class="form-control autofocus" />
<input type="text" data-bind="value: senha" placeholder="Senha" class="form-control autofocus" />
</form>
</div>
<div data-bind="if: $parent.loading()">
<img src="img/loading.gif"/>
</div>
<div class="modal-footer">
<button class="btn btn-success" data-bind="click: ok, active: $parent.loading()">
Login
</button>
</div>
</div>
请注意,我将您的 if
绑定(bind)更改为:
"if: loading()"
用括号引用loading
。这使用提供给可观察量的默认值执行立即评估,然后在可观察量发生变化时重新评估。
此外,可能需要将 "click: ok, active: $parent.loading()"
更改为 click: $parent.ok.bind($parent), active :$parent.loading()
。输入 #ok 函数时,使用调试器检查您的上下文。
关于逻辑的注释:在我看来,您在模式页脚中的含义可能是
active: !$parent.loading()
当表单加载数据时,“确定”按钮真的应该处于事件状态吗?
loginBox.js(模块实例方法)
define (
[
'plugins/dialog',
'knockout'
],
function (
dialog,
ko) {
var LoginBox = function () {
this.login = '';
this.senha = '';
this.loading = ko.observable(false);
};
LoginBox.prototype.ok = function () {
var _vm = this;
this.loading(true);
$.ajax( {
type: "post",
data: { "LoginForm[cpf]" : this.login, "LoginForm[password]" : this.senha , 'ajax':'login-form' },
url: localStorage['baseurl']+localStorage['router']+'site/login',
success: function (data) {
console.log(data);
},
error: function (request, status, error) {
console.log(request.status);
console.log(status);
console.log(error);
},
complete: function (data) {
alert('hqweuiqhioehqio');
_vm.loading(false);
}
});
};
LoginBox.prototype.show = function() {
dialog.show(this);
};
return LoginBox;
};
);
请注意我对 this.loading
的处理。它是一个可观察量,并且可观察量是使用我上面展示的方法进行更新的(记住,它们是函数)。当您以这种方式分配 true
时(this.loading = true
),您将覆盖可观察对象本身并将其转换为不可观察标量。因此,当值稍后更改(从 false 到 true 再到 false)时, View 不会更新。
另请注意,您必须导入 KnockoutJS。
另一个问题:您的 #complete 函数中存在 this
引用问题。请注意,我在 #Ok 函数的顶部执行此操作:
var _vm = this; //Some people are inclined to this format: var that = this;
然后,在您的 #complete 函数中,我执行以下操作:
_vm.loading(false);
在 #complete 函数中使用 this
引用 #complete 函数本身,而不是 viewModel。我们必须在 #complete 函数外部保存对 this
的引用。
还有另一个问题:#show 不在原型(prototype)上。
关于javascript - 实现自定义加载消息 DURANDAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27180371/
Nuget 说我已经为我正在检查的项目安装了 Durandal Transitions 包,但是 ~/Scripts/durandal/transitions仅包含默认过渡 entrance.js 其
每次我读到有关 Javascript 的新内容时,我都会阅读有关 Durandal 和 Aurelia 的内容。我在很多博客上听到开发人员要求从 Durandal 升级到 Aurelia 或类似的版本
在我基于 HotTowel 的项目中,我使用 Durandal 1.1.1 进行路由。 我有一个登录路由,如果用户未登录,我想激活该路由。 我的路线配置如下: var routes = [ {
每次我读到有关 Javascript 的新内容时,我都会阅读有关 Durandal 和 Aurelia 的内容。我在很多博客上听到开发人员要求从 Durandal 升级到 Aurelia 或类似的版本
Durandal route 星号的用途是什么?这是来自 the Docs 的示例: route:'knockout-samples*details' 编辑:使用 Durandal 2.0 最佳答案
Durandal 路由中星号的用途是什么?这是来自 the Docs 的示例: route:'knockout-samples*details' 编辑:使用 Durandal 2.0 最佳答案 您引用
我的 Durandal 路由配置如下。 var routes = [ ....... More Routes Here..... { url: 'login', moduleId: '
我正在使用 Durandal 2.0。我有一个搜索 View ,想将所选项目传递到详细信息 View 。我知道如何传递 Id,但由于在这种情况下搜索具有整个对象,因此我想在导航时传递对象。我以为我可以
我搜索如何在撰写数据绑定(bind)上停用转换, 因为我在 chrome 上的内容 html 并不美观而且小错误。 因为我在过渡时有一个滚动水平打印,并且在移动鼠标后消失了。 内容在 iFrame 中
在我的 shell 中,我按照以下方式设置了路线: router.map([ { route: '', title: 'Search', moduleId: 'viewmode
我正在尝试使用 John Papa 的 HotTowel 模板实现 SPA。我可以做多少级的 ko compose 有限制吗? 在它调用的 shell.html 中 在我的事件 View 中,我尝试
我需要帮助在 Durandal 路由器中动态添加/删除路由。我想要的是在用户登录后,我将能够根据登录用户的类型添加或删除特定路线。 我试图从 visibleRoutes/allRoutes 数组中添加
我正在看的文章是Durandal Router . 下 mapAuto ,它说我们可以在 url #/customers/1 中传递 splat 参数.和 activate viewmodel 中的函
我正在查看这些路由信息对象结构的文档,但没有找到任何内容。由于 Durandal 使用其他 JS 库,我不确定它属于什么(也许是 sammy?)。 我面临两个问题:问题 #1 我想在路线信息中使用一个
我有一个现有的 Durandal.js 应用程序,它利用了所有 ES5 功能,但我想将它升级到新的 Aurelia 平台。什么是正确的升级路径?最简单的升级方式是什么?是否有包含升级路径的文档? 最佳
我正在开发 Hottowel 项目,并更新到 Durandal 2.0。我已经关注了Converting from 1.x to 2.0 guide ,但我仍然有一些问题: 1.加载router插件总
在我看来......这种方式的初始路线是通过以下方式定义的: { route: '', moduleId: 'viewmodels/customers', title: 'customers', na
我想知道是否有人在 Durandal.js 上遇到过类似的问题。 我收到间歇性的“未找到 View 。搜索”错误,很难复制,因为只是偶尔发生。 当 createView 函数无法定位 View 时,它
当您在 SPA 代码中的某处遇到脚本错误时,整个应用程序变得完全没有响应并且您无法导航到任何其他页面(视图)。似乎 durandal 只是卡住了! (唯一的出路是点击刷新再次启动SPA) 在 Du
两者有什么区别,什么时候适合使用它们?我已经看过文档,但还没有完全弄清楚。 最佳答案 这两个事件都是 Durandal 生命周期回调的一部分。 activate() 允许新对象执行自定义激活逻辑。 (
我是一名优秀的程序员,十分优秀!