我一直在尝试寻找一种有效的方法,但我已经无处可去.我有一个对象数组,如下所示:
array[i].id = some number;
array[i].name = some name;
Run Code Online (Sandbox Code Playgroud)
我想要做的是找到id等于的对象的INDEXES,例如,0,1,2,3或4中的一个.我想我可以做类似的事情:
var indexes = [];
for(i=0; i<array.length; i++) {
(array[i].id === 0) ? { indexes[0] = i }
(array[i].id === 1) ? { indexes[1] = i }
(array[i].id === 2) ? { indexes[2] = i }
(array[i].id === 3) ? { indexes[3] = i }
(array[i].id === 4) ? { indexes[4] = i }
}
Run Code Online (Sandbox Code Playgroud)
虽然这可行,但它看起来相当昂贵且缓慢(更不用说丑陋),特别是如果array.length可能很大.关于如何修饰这一点的任何想法?我想过以某种方式使用array.indexOf,但我没有看到如何强制语法.这个
array.indexOf(this.id === 0);
Run Code Online (Sandbox Code Playgroud)
例如,返回undefined,因为它可能应该.提前致谢!
对于select.js选择菜单插件,是否有一种既定方法可以将"选择列表中的所有项目"或"将列表中的所有项目"功能添加到多选输入?在主分支或者可能是其中一个分叉?或者有人这样做之前有一些提示吗?
在我的meteor.js应用程序中,我正在尝试编写一个简单的管理页面,可以通过他/她的电子邮件地址找到用户.
我可以看到,在Meteor.users集合中有一个"电子邮件"数组,其中包含这样的对象
{ address : 'foo@foo.com',
verified : false
}
Run Code Online (Sandbox Code Playgroud)
通常在Mongodb中我可以在这个'电子邮件'数组中搜索,如下所示:
Meteor.users.find({ emails.address : 'foo@foo.com' });
Run Code Online (Sandbox Code Playgroud)
但是这个查询引发了一个错误:
While building the application:
client/admin.js:224:41: Unexpected token .
Run Code Online (Sandbox Code Playgroud)
Aka Meteor不喜欢嵌套查询...
有关如何通过电子邮件地址查询Meteor.users集合的任何想法?
在我的一些Meteor方法中,我将从客户端发送Mongodb ObjectId作为参数.我想通过Meteor的check()系统来运行它们,但我似乎找不到任何与它们成功匹配的东西.
我试过了
var someObjectId = Meteor.Collection.ObjectId();
check(someObjectId, Meteor.Collection.ObjectId()) // fails
check(someObjectId, { _str : String }) //fails
check(someObjectId, String) //fails
Run Code Online (Sandbox Code Playgroud)
任何帮助非常感谢!
我想知道其他人如何处理将redux动作创建者从智能顶级组件传递到许多较低级别的哑组件而不会使他们的道具定义膨胀.
例如,遵循这个关于redux的优秀教程,如果我将动作创建者列表传递给道具,就像这样
import Voting from './Voting';
import * as actionCreators from '../action_creators';
...
export const VotingContainer = connect(
mapStateToProps,
actionCreators
)(Voting);
Run Code Online (Sandbox Code Playgroud)
然后在我的Voting组件中,我可以访问actionCreators,这非常酷.
但是,如果我说20个用于投票的actionCreator及其所有子组件,例如.
Voting -> VotingContainer -> VotingDetail -> VotingFoo -> VotingBar
Run Code Online (Sandbox Code Playgroud)
然后我最终得到了这样的渲染函数
class Voting extends React.Component {
render(){
<VotingContainer
actionCreator1={this.props.actionCreator1}
.
.
.
actionCreator15={this.props.actionCreator15} />
}
}
class VotingContainer extends React.Component {
render(){
<VotingDetail
actionCreator1={this.props.actionCreator1}
.
.
.
actionCreator12={this.props.actionCreator12} />
}
}
.
.
.
class VotingFoo extends React.Component {
render(){
<VotingBar
actionCreator1={this.props.actionCreator1}
.
.
.
actionCreator6={this.props.actionCreator6} …Run Code Online (Sandbox Code Playgroud) 我有一些带有属性data-foo和data-bar的DOM元素.
是否有一种优雅的方式只返回那两个属性匹配的元素?
目前我只是使用过滤器,但也许有更好的方法
var result = $('[data-foo="aaa"]').filter('[data-bar="bbb"]');
Run Code Online (Sandbox Code Playgroud) 使用velocity/jasmine,我有点不知道应该如何测试服务器端方法,要求当前登录的用户.有没有办法让Meteor认为用户是通过stub/fake登录的?
myServerSideModel.doThisServerSideThing = function(){
var user = Meteor.user();
if(!user) throw new Meteor.Error('403', 'not-autorized');
}
Jasmine.onTest(function () {
describe("doThisServerSideThing", function(){
it('should only work if user is logged in', function(){
// this only works on the client :(
Meteor.loginWithPassword('user','pwd', function(err){
expect(err).toBeUndefined();
});
});
});
});
Run Code Online (Sandbox Code Playgroud) 我在客户端上使用Backbone.js,在后端使用node.js,我在执行'有限'模型保存时遇到了一些麻烦,如下所述:http://backbonejs.org/#Model-保存
如在示例中,如果我这样做
book.save({author: "Teddy"});
Run Code Online (Sandbox Code Playgroud)
如何使用express.js访问save的参数,即我只想保存到'author'字段?我尝试了以下内容
req.body -> gives ALL parameters of the model being saved, I want only the 'author' field
req.query -> empty
Run Code Online (Sandbox Code Playgroud)
任何帮助非常感谢!
在我的meteor应用程序中,我使用Session来存储有关用户活动的临时信息.我想通过amplify.js将这些信息的某些部分保存到浏览器中,但不是全部.
我想要一种方法来获得'临时'会话密钥和'持久'会话密钥.我可以打电话
Session.set('persistent', 'this is persisted to browser memory');
Session.set('temporary', 'this will be erased on page reload, etc');
Run Code Online (Sandbox Code Playgroud)
然后在页面重新加载后
Session.get('persistent'); // returns 'this is persisted to browser memory'
Session.get('temporary'); // returns undefined
Run Code Online (Sandbox Code Playgroud)
我在SO上发现了一个相关的帖子,但是这样可以保存这个方法,它会持续整个Session对象,我不想这样做.另外,我不想为此使用MongoDB,我希望存储只是纯粹的客户端...
提前谢谢了!
我想在Backbone中实现可逆动画,就像我们在jquery中一样:
$('a.contact').toggle(
function(){
// odd clicks
},
function(){
// even clicks
});
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在骨干的事件语法中做到这一点?怎么做我模仿功能,功能设置?
events : {
'click .toggleDiv' : this.doToggle
},
doToggle : function() { ??? }
Run Code Online (Sandbox Code Playgroud)