小编Gor*_*ven的帖子

在Javascript中循环使用回调

我试图写在这里给出的伪代码https://dev.twitter.com/docs/misc/cursoring使用节点的OAuth使用javascript https://github.com/ciaranj/node-oauth.但是我担心由于回调函数的性质,游标永远不会分配给next_cursor,循环只会永远运行.谁能想到解决这个问题?

module.exports.getFriends = function (user ,oa ,cb){
  var friendsObject = {};
  var cursor = -1 ;
  while(cursor != 0){
    console.log(cursor);
      oa.get(
        'https://api.twitter.com/1.1/friends/list.json?cursor=' + cursor + '&skip_status=true&include_user_entities=false'
        ,user.token //test user token
        ,user.tokenSecret, //test user secret
        function (e, data, res){
          if (e) console.error(e);
          cursor = JSON.parse(data).next_cursor;
          JSON.parse(data).users.forEach(function(user){
            var name = user.name;
            friendsObject[name + ""] = {twitterHandle : "@" + user.name, profilePic: user.profile_image_url};
          });        
          console.log(friendsObject);   
        }
      );
    }  
  }
Run Code Online (Sandbox Code Playgroud)

javascript twitter node.js twitter-oauth

4
推荐指数
1
解决办法
8410
查看次数

Backbone View渲染功能中的Ajax请求

我想在渲染骨干视图之前传递一个额外的变量(userid).我正在使用ajax请求获取额外变量,但因为是异步的,我认为我的页面在获取变量之前正在呈现.为简单起见,我想在骨干视图中有这个:

PostsApp.Views.Post = Backbone.View.extend({
    template: _.template($('#post-template').html()),

    render: function(){
        var newObject = this.model.toJSON();
        $.ajax({
            url:"/user",
            success:function(result){
                newObject.twittername = result.name; ;
            }
        });
        this.$el.html(this.template(newObject));
    }

});
Run Code Online (Sandbox Code Playgroud)

我想我可以把它.$ el.html(this.template(newObject)); 在回调中,但是"这意味着什么......有人能想到解决这个问题吗?

或者在渲染函数中发送这样的请求是完全非常糟糕的.

ajax jquery backbone.js backbone-views

3
推荐指数
1
解决办法
1万
查看次数

可以在Mongoose中相互引用两个模式吗?

我有两个模式,我希望能够从另一个模式访问它们.我正在尝试做这样的事情:

//email.js

var mongoose = require('mongoose')
    ,Schema = mongoose.Schema
    , FoodItemSchema = require('../models/fooditem.js')
    , UserSchema = require('../models/user.js').schema
    , User = require('../models/user.js').model

    console.log(require('../models/user.js'));

    var emailSchema = new Schema({
        From : String,
        Subject : FoodItemSchema,
        Body : String,
        Date: Date,
        FoodItems : [FoodItemSchema],
        Owner : { type : Schema.Types.ObjectId , ref: "User" }
    });

    module.exports = {
        model: mongoose.model('Email', emailSchema),
        schema : emailSchema 
    }
Run Code Online (Sandbox Code Playgroud)

//user.js

var mongoose = require('mongoose')
    ,Schema = mongoose.Schema
    , Email = require('../models/email.js').model
    , EmailSchema = require('../models/email.js').schema


console.log(require('../models/email.js'));

var …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose mongodb node.js

3
推荐指数
1
解决办法
3460
查看次数

如何将最新帖子放在骨干上

这就是我的集合视图的样子,我如何在集合中安排我的模型,以便最近添加的模型呈现在顶部?

PostsApp.Views.Posts = Backbone.View.extend({
    initialize: function(){
        this.listenTo(this.collection, 'add', this.addOne);

    },
    render: function(){
        this.collection.forEach(this.addOne, this);
    },

    addOne: function(post){
        var postView = new PostsApp.Views.Post({model:post, collection :this.collection});
        postView.render();
        this.$el.append(postView.el);
    }
});
Run Code Online (Sandbox Code Playgroud)

编辑:前置方法似乎工作,但我也尝试过这样的比较器,它似乎没有工作,我的问题是什么?

PostsApp.Models.Post = Backbone.Model.extend({
    urlRoot : '/tweet',
    idAttribute: '_id',
    defaults:{
        name: '',
        adress: '',
        pictureUrl: '',
        postListing: '',
        comments: '',
        date_created: new Date()
    }
    }
});

PostsApp.Collections.Posts = Backbone.Collection.extend({
    model: PostsApp.Models.Post,
    url: '/tweet',
    comparator: function(post){
        return post.get("date_created");
    }
});
Run Code Online (Sandbox Code Playgroud)

backbone.js backbone-views

2
推荐指数
1
解决办法
1067
查看次数

Socket.io和Backbone

我正在尝试学习socket.io,我决定将socket.io添加到我之前构建的骨干应用程序中.

所以使用require.js我在我的主app.js文件中有以下内容:

require(
    ["jquery",
    "underscore",
    "backbone",
    "bootstrap",
    "view/postsview",
    "model/model",
    "collection/collection",
    "socketio",


],
  function($, _, B,boot, postsView, model, collection, io) {

   $(function() {


        window.socket = io.connect('http://127.0.0.1');

        var postmodel = new model();
        var postcollection = new collection();

        window.socket.on('newPost', function (data) {
            postcollection.create(data);
        });

        var posts = new postsView({model:postmodel, collection:postcollection});
        posts.render();
        $(".maincontainer").html(posts.el);
    }
});
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我的套接字是一个全局变量.因为我想在我的backbone.view中向这个套接字发出一些东西.我对javascript很新,我知道使用全局变量并不总是最好的做法.

我的骨干视图看起来像:

var FormView = Backbone.View.extend({
    template: _.template(formTemplate),

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

    events:{
        'click #submit-button' : 'save'
    },

    save: function(e){
        e.preventDefault();
        var newname = this.$('input[name=name-input]').val();
        var newadress = this.$('input[name=adress-input]').val(); …
Run Code Online (Sandbox Code Playgroud)

node.js backbone.js socket.io

2
推荐指数
1
解决办法
3351
查看次数

使用内联哈希定义Perl while()=每个{}循环

我试图在每个循环中定义一个内联哈希,我的程序不会抛出任何错误但也不会执行print语句.是否可以定义内联哈希,如下所示:

while (my (key, value) = each %{ (apple  => "red", orange => "orange", grape  => "purple")}) { 
    print "something";
}
Run Code Online (Sandbox Code Playgroud)

或者我不能在每个循环工作时,如果我直接调用每个返回如下所示的散列的语句中的sub:

sub returnsHash {
    my %fruits = (
        apple  => "red",
        orange => "orange",
        grape  => "purple",
    );
    return %fruits;
}

while (my (key, value) = each %{ returnsHash() }) { 
    print "something";
}
Run Code Online (Sandbox Code Playgroud)

perl

2
推荐指数
2
解决办法
360
查看次数

使用ObjectId的Mongoose中的嵌套数组

我正在尝试记录用户投票的帖子的ID,我正在为此创建一个架构,如下所示:

var userSchema = new Schema({
    twittername: String,
    twitterID: Number,
    votedPosts: [{ObjectId : {votetype : Number}} ]
  });
Run Code Online (Sandbox Code Playgroud)

用户被完美地创建,我想在用户投票时更新votedPosts属性,所以我打电话:

User.update({twitterID : req.body.userID} , { $push : {votedPosts : {postID : {votetype: newvotetype }}}} ,function (err, user, raw) {
    if (err){ 
        console.log(err);
    }
    else{
        console.log('user ' + user);
    }

});
Run Code Online (Sandbox Code Playgroud)

不幸的是结果不像我在模式中描述的那样,我得到了这个:

  db.users.find().pretty()
    {
        "__v" : 0,
        "_id" : ObjectId("51bf4ef8dbda2f2e0c000001"),
        "twitterID" : 102016704,
        "twittername" : "gorkemyurt",
        "votedPosts" : [
            {
                "_id" : ObjectId("51bf5e48c3ffefe20c000002")
            }
        ]
    }
Run Code Online (Sandbox Code Playgroud)

我很困惑mongoose在votedPosts数组中添加的额外"_id"..为什么不能将ObjectId("51bf5e48c3ffefe20c000002")作为键值对的键?

javascript mongoose mongodb node.js

1
推荐指数
1
解决办法
1559
查看次数

继续添加到NSMutable数组

我是Objective-C的新手,并尝试使用表视图创建待办事项列表应用程序.当按下按钮时,我试图继续向我的可变数组添加字符串.但每次按下按钮时,只有最后一个字符串被添加到数组中.

- (IBAction)notebutton:(UIButton *)sender {

   NSMutableArray *mystr = [[NSMutableArray alloc] init];
   NSString *name = _noteField.text;
   [mystr addObject:name];
   [self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsmutablearray ios

0
推荐指数
1
解决办法
98
查看次数

获取所有没有id的div

什么是最有效的方法来获取页面上没有id的所有div使用jquery或只是vanilla js,如果它更有效.我知道我可以获取所有div并循环遍历它们并检查它们是否设置了ID.我很好奇DOM API或Jquery是否有优化的方法.

javascript jquery dom

0
推荐指数
1
解决办法
58
查看次数