小编Gle*_*ift的帖子

JavaScript 原生 Promise() 不带回调

看看这个 jQuery 代码:

var promise = new Deferred(),
    some;

some = function(promise) {
    // do cool things

    promise.resolve();
};

promise.then(/*  callback cool things   */);

// init everything
some(promise);
Run Code Online (Sandbox Code Playgroud)

我不确定这种方法的架构正确性,但我使用它很长时间并且对我来说很方便。

在原生 JavaScript 中我不能使用这种方法。构造函数new Promise()需要回调参数,因此我无法将实例Promise作为参数传递。

所以我的问题是:如何预定义 JavaScript 原生 Promise,将其作为参数传递给函数和解析?

javascript jquery design-patterns promise ecmascript-harmony

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

获得真实设备的屏幕尺寸

获取真实设备的屏幕尺寸时遇到问题.看看jsfiddle:http://jsfiddle.net/glenswift/CpBkU/

调整输出框的大小会导致h1文本发生更改.我知道h1的不同部分的像素大小,所以它在桌面上正常工作.但是在具有大dpi和小屏幕的设备上,我的解决方案不起作用,文本消失了.

我不知道如何解决这个问题.我见过simular问题(1,2),但他们不帮我.

我的代码:

function setPositions(){
    var W = $(window).width(),
        h1 = '';
    if (W < 210) {
        h1 = 'Hello';
    } else if (W < 270) {
        h1 = 'Hello World';
    } else {
        h1 = 'Hello World Text'
    }
    $('h1').empty().text(h1);
}
$(document).ready(function(){
    setPositions();
});
$(window).resize(function(){
    setPositions();
});
Run Code Online (Sandbox Code Playgroud)

非常感谢.

javascript jquery responsive-design

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

如果密钥不存在则将值推送到数组Mongoose

给定mongoose架构

var SomeSchema = new Schema({
    // ...
    members: [
        {
            name: String,
            username: String
        }
    ]
});
Run Code Online (Sandbox Code Playgroud)

从我的代码我想推送对象,members但只有在数组中没有给定的用户名时.我怎么能用猫鼬做呢?

database mongoose mongodb nosql node.js

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

如何在knex.js中使用postgres :: date

我有一列type timestamp。我需要选择给定日期的所有记录。在sql中,它类似于:

select * from "table" where "date"::date = '2015-08-22';
Run Code Online (Sandbox Code Playgroud)

我尝试了以下操作:

db('table').select().where('date::date', '=', date);
Run Code Online (Sandbox Code Playgroud)

但这会引发错误

错误:从“表”中选择*,其中“日期::日期” = $ 1-列“日期::日期”不存在

因为knex位置引用错误。

有什么方法可以执行这种查询吗?还是我应该使用whereRaw

postgresql date node.js knex.js

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

Excel图表平滑算法

我需要在javascript中以程序方式平滑我的"图表构建器".这样做的Excel变体非常好,但我没有建议使用什么算法.我尝试通过编写宏来获得VBA代码,但我所拥有的只是(预期的)ActiveChart.SeriesCollection(1).Smooth = True

有人知道Microsoft Excel使用什么算法来平滑图表或查看代码的方式吗?

在此输入图像描述

UPD:对于那些发现这个问题并且有同样问题的人,我可以推荐这个带有javascript源代码的SVG解决方案

algorithm excel charts vba excel-vba smoothing

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

Angular.js resourceProvider返回带有数据的$ promise

给出angular.js服务:

angular.module('mymodule').factory('Products', ['$resource',
    function($resource) {
        return $resource('/path/to/getProducts', {}, {
            find: {
                method: 'GET',
                isArray: false
            }
        });
    }
]);
Run Code Online (Sandbox Code Playgroud)

mymodule我的控制器中find查询:

$scope.findAll = function () {
    Products.find(function (products) {
        console.log(Object.keys(products));
        // ['prodA', 'prodB', ... , '$promise', '$resolved']
        for (var i in products) {
            if (!products.hasOwnProperty(i)) continue;
            console.log(products[i].description.someprop);
            // Got error: Cannot read property 'someprop' of undefined
            // Trust me: I debug this place. someprop is defined for all items except that two
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

它工作正常,但返回$ promise和$使用数据集解析,因此我无法遍历我的数据. …

javascript rest resourceproviderfactory angularjs angular-services

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

Knex 从多个表中选择

我想用 knex 运行以下 SQL:

select * from (
  (select * from foo)
  union all
  (select * from bar)) as biz limit 10 offset 20;
Run Code Online (Sandbox Code Playgroud)

有没有办法不用knex.raw

javascript postgresql node.js bookshelf.js knex.js

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

试剂不会在let内部使用deref-rerender组件

我有原子foo:

(defonce foo (r/atom "foo"))
Run Code Online (Sandbox Code Playgroud)

我有父组件:

(defn parent-component []
  (js/setTimeout #(reset! foo "bar") 5000)
  (child-component {:foo foo}))
Run Code Online (Sandbox Code Playgroud)

我有孩子组成部分:

(defn child-component [props]
  (let [derefed (deref (:foo props))]
    (fn []
      [:div
       [:p derefed]
       [:p (deref (:foo props))]])))
Run Code Online (Sandbox Code Playgroud)

重置后只更新第二段foo.

为什么这样工作?

javascript clojure clojurescript reagent re-frame

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

通过require.js导出新对象时:它实际上是创建"新"对象还是仅返回现有实例

假设我在require.js中有模块A,B和C. 模块A导出新对象.

define(function() {

    // constructor
    function F() {
        this.prop = 'some';
    }

    // module exports
    return new F();   

});
Run Code Online (Sandbox Code Playgroud)

模块B和C从A导入F()的实例:

define(['A'], function(f_inst) {
    // code
});
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我需要F成为单身人士.我还没有认真了解require.js的工作原理.所以,这是我的问题:在这种情况下,我是否需要为F()使用单例模式?

javascript singleton module requirejs

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

如何在jquery回调函数中访问reactjs组件状态

如何从jQuery"changeDate"事件(jqueryUI date)回调函数访问反应组件状态.

var subheader = React.createClass({

    getInitialState: function () {
        return {
            msg: this.props.msg
        };
    },

    componentDidMount: function () {
        $('#date').datepicker({
            format: "dd/mm/yyyy"
        }).on("changeDate", function (e) {
            console.log(this.state.msg);
        });

    },
    render: function () {
        return (
                    // render HTML here.

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

但得到this.state是未定义的.请任何人帮助我.

jquery reactjs

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