小编Dan*_*iel的帖子

Backbone.js - id vs idAttribute vs cid

我已经研究了Backbone.js几周了,我觉得使用模型,路由器和集合的视图很舒服.

我还有一些很大的差距:

  1. 什么是之间的连接id,cididAttribute?它们如何相互影响?

  2. 什么时候新模型获得其ID?服务器负责分配它吗?我是否需要将其添加到defaults模型中(可能作为一个函数)?也许addNewModel功能应该这样做?

javascript ajax jquery backbone.js underscore.js

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

JavaScript构造函数中的"var"变量会发生什么?

例:

function Foo() {
    this.bla = 1;
    var blabla = 10;
    blablabla = 100;
    this.getBlabla = function () { 
        return blabla; // exposes blabla outside
    }
}

foo = new Foo();
Run Code Online (Sandbox Code Playgroud)

原始问题:

我知道bla将分配给Foo的每个实例.会发生什么blabla

新问题:

我现在明白了:

this.bla = 1;     // will become an attribute of every instance of FOO.
var blabla = 10;  // will become a local variable of Foo(**not** an attribute of every    instance of FOO), which could be accessed by any instance of FOO …
Run Code Online (Sandbox Code Playgroud)

javascript constructor var

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

导出mongoose数据库模块

我需要导出我的mongoose数据库模块,所以我可以使用我程序中每个模块的定义模型.

例如,我的database.js模块看起来像这样:

var mongoose = require('mongoose'),
    db = mongoose.createConnection('mongodb://localhost/newdb'),
    Schema = mongoose.Schema;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
    console.log("Connected to database newdb");

    var dynamicUserItemSchema = new mongoose.Schema({
      userID: Number,
      rank:  Number,
    });

    var staticUserItemSchema = new mongoose.Schema({
        _id: Schema.Types.Mixed,
        type: Schema.Types.Mixed,
    });

    var DynamicUserItem = db.model('DynamicUserItem', dynamicUserItemSchema);
    var StaticUserItem = db.model('StaticUserItem', staticUserItemSchema);

});
Run Code Online (Sandbox Code Playgroud)

我想能够添加var db = require('../my_modules/database');到我的程序的任何其他模块 - 所以我将能够使用这样的模型:

db.DynamicUserItem.find(); 要么 item = new db.DynamicUserItem({});

是否可以使用"出口"或"模块出口"来做到这一点?谢谢.

javascript mongoose mongodb node.js express

7
推荐指数
2
解决办法
4万
查看次数

将对象文字转换为已排序的数组

我有一个对象文字,其键的值是更多的对象,内部对象的一个​​键被命名为"rank" - 并具有浮点值.我想将对象文字转换为内部对象的数组,按"rank"的值排序.

输入对象:

{
452:{
     bla:123,
     dff:233,
     rank:2
  },
234:{
     bla:123,
     dff:233,
     rank:1
  }
Run Code Online (Sandbox Code Playgroud)

}

输出数组:

[
 { bla:123, dff:233, rank:1},
 { bla:123, dff:233, rank:2 }
]
Run Code Online (Sandbox Code Playgroud)

javascript arrays sorting

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

Bluebird.js自定义错误捕获功能,不适用于第一个承诺?

我正在尝试使用Bluebird.js的自定义错误处理程序.

在下面的示例中,调用了catch-all处理程序,而不是MyCustomError处理程序,但是当我将拒绝移动到then函数(并解析了firstPromise ...)时,调用了MyCustomError处理程序.这是为什么?弄错了?谢谢.

var Promise = require('bluebird'),
debug = require('debug')('main');

firstPromise()
    .then(function (value) {
      debug(value);
    })
    .catch(MyCustomError, function (err) {
      debug('from MyCustomError catch: ' + err.message);
    })
    .catch(function (err) {
      debug('From catch all: ' + err.message);
    });

/*
 * Promise returning function.
 * */
function firstPromise() {
  return new Promise(function (resolve, reject) {
    reject(new MyCustomError('error from firstPromise'));
  });
}
/*
 *  Custom Error
 * */
function MyCustomError(message) {
  this.message = message;
  this.name = "MyCustomError";
  Error.captureStackTrace(this, MyCustomError);
}
MyCustomError.prototype …
Run Code Online (Sandbox Code Playgroud)

javascript error-handling promise bluebird

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

"var"变量,"this"变量和"global"变量 - 在JavaScript构造函数中

在我的上一个问题之后,这个对我来说更准确:

例:

function Foo() {
    this.bla = 1;
    var blabla = 10;
    blablabla = 100;
    this.getblabla = function () { 
        return blabla; // exposes blabla outside
    }
}
foo = new Foo();
Run Code Online (Sandbox Code Playgroud)

我现在明白了:

this.bla = 1;     // will become an attribute of every instance of FOO.
var blabla = 10;  // will become a local variable of Foo(will **not** become an attribute of every instance of FOO), which could be accessed by any instance of FOO - only if there's …
Run Code Online (Sandbox Code Playgroud)

javascript constructor var this

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

内部forEach循环内部的ajax,在内部回调触发后完成

我有一个ajax调用,它位于另一个函数内的forEach循环中.问题是,外部函数的回调在内部循环结束之前触发 - 因此"staticItemList"在传递给回调时没有填充项目.我该如何解决这个问题?我真的花了很多时间.谢谢.

  exports.buildTheList = function (parsedList, callback) {
        var staticItemList = {};
        parsedList.forEach(function(parsedItem) { 

                db.staticList.find({"_id":parsedItem.ID}).forEach(function(err, doc) {
                    if (!doc){
                        console.log("newStaticItem not found in db");
                        parsedDataFetcher.fetchParsedDetails(Path, function(parsedDetails){
                            staticItemList["_id"] = parsedItem.ID;
                            staticItemList[parsedItem.ID] = {
                                    "_id": parsedItem.ID,
                                    "type": parsedItem.type,
                                     } 
                                 })

                             }else {
                                   console.log("newStaticItem already found in db");
                             } 
                 });
             });


         callback(staticItemList);
     }
Run Code Online (Sandbox Code Playgroud)

javascript ajax node.js

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

在悬停时显示文本时使img变暗

我的网站中的一些图像在悬停时需要变暗,同时也需要在悬停之前显示隐藏的文本(文本将显示在变暗图像的顶部).

我已经用这种方式实现了img-darken部分 - http://jsfiddle.net/4Dfpm/.

实现"在悬停(同一悬停)上公开文本"部分的好方法是什么?它只能用CSS完成,或者这次我需要使用脚本吗?

谢谢.

**img-darken部分如何实施:

?

a.darken {
    background: black;
}

a.darken img {
    display: block;
    transition: all 0.5s linear;
}

a.darken:hover img {
    opacity: 0.7;
}
Run Code Online (Sandbox Code Playgroud)

javascript css jquery hover css3

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

理解Python中的"自我"

我在udacity.com上看到了这个例子:

def say_hi():
    return 'hi!'

i = 789

class MyClass(object):

    i = 5

    def prepare(self):
        i = 10
        self.i = 123
        print i

    def say_hi(self):
        return 'Hi there!'

    def say_something(self):
        print say_hi()

    def say_something_else(self):
        print self.say_hi()
Run Code Online (Sandbox Code Playgroud)

输出:

>>> print say_hi()
hi!
>>> print i
789
>>> a = MyClass()
>>> a.say_something()
hi!
>>> a.say_something_else()
Hi there!
>>> print a.i
5
>>> a.prepare()
10
>>> print i
789
>>> print a.i
123
Run Code Online (Sandbox Code Playgroud)

我理解一切,除了为什么a.say_something()等于hi!和不等Hi there!.这对我来说很奇怪,因为它say_something() …

python oop self python-2.7

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