te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - namespaces in coffeescript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - namespaces in coffeescript - Stack Overflow

programmeradmin3浏览0评论

I'd like to use namespaces as one would in javascript by using the keyword "with", but CoffeeScript reports this as a reserved keyword and refuses to pile is there any way I could use namespaces in cs?

In particular, I want to include a CoffeeScript file dynamically (trusted source), like loading models for a database schema, but I want the included script to have access to a local namespace.


Edit:

Here's what I want to do. I am setting up a web framework that maps the directory tree to an application based on express and mongoose. For example, there's a sub-directory 'models' that contains a file 'user.coffee' with code like this inside:

name:
    type: String
    unique: on
profiles: [ Profile ]

Whereby Profile is a class that sits in a local object named model. When the user model is being loaded, I wanted it to access the model classes that sit in my local model store.

My work-around for now was to write model.Profile into the file 'user.coffee'. Hope it is clear what I mean.


2nd Edit

Here's how I did it without using with:

user.coffee

name:
    type: String
    unique: on
profiles: [ @profile ]

profile.coffee

content: String

And here's how it's dynamically loaded:

for fm in fs.readdirSync "#{base}/models"
    m = path.basename fm, '.coffee'
    schema[m] = (()->
        new Schema coffee.eval (
            fs.readFileSync "#{base}/models/#{fm}", 'utf8'
        ), bare: on
    ).call model
    mongoose.model m, schema[m]
    model[m] = mongoose.model m

Seems an okay solution to me.

I'd like to use namespaces as one would in javascript by using the keyword "with", but CoffeeScript reports this as a reserved keyword and refuses to pile is there any way I could use namespaces in cs?

In particular, I want to include a CoffeeScript file dynamically (trusted source), like loading models for a database schema, but I want the included script to have access to a local namespace.


Edit:

Here's what I want to do. I am setting up a web framework that maps the directory tree to an application based on express and mongoose. For example, there's a sub-directory 'models' that contains a file 'user.coffee' with code like this inside:

name:
    type: String
    unique: on
profiles: [ Profile ]

Whereby Profile is a class that sits in a local object named model. When the user model is being loaded, I wanted it to access the model classes that sit in my local model store.

My work-around for now was to write model.Profile into the file 'user.coffee'. Hope it is clear what I mean.


2nd Edit

Here's how I did it without using with:

user.coffee

name:
    type: String
    unique: on
profiles: [ @profile ]

profile.coffee

content: String

And here's how it's dynamically loaded:

for fm in fs.readdirSync "#{base}/models"
    m = path.basename fm, '.coffee'
    schema[m] = (()->
        new Schema coffee.eval (
            fs.readFileSync "#{base}/models/#{fm}", 'utf8'
        ), bare: on
    ).call model
    mongoose.model m, schema[m]
    model[m] = mongoose.model m

Seems an okay solution to me.

Share Improve this question edited Jun 15, 2011 at 18:04 Johann Philipp Strathausen asked Jun 15, 2011 at 15:12 Johann Philipp StrathausenJohann Philipp Strathausen 5,7364 gold badges32 silver badges39 bronze badges 5
  • I'm afraid your edit hasn't made your intentions much clearer. Could you add representative code for model.Profile and how you would like to use it? – lawnsea Commented Jun 15, 2011 at 15:54
  • Stick an @ in front of Profile when defining it: class @Profile. Since this is global, that'll make it a global named Profile. No changes needed to user.coffee (though you could write @Profile to make it clearer that you're referencing a global). – Trevor Burnham Commented Jun 15, 2011 at 16:02
  • Profile is a mongoose Schema, and it is defined dynamically the same way User is: via a CoffeeScript file. profile.coffee in this case. See github./LearnBoost/mongoose and mongoosejs. - so I cannot define it globally. Despite that, I wanted to avoid global definitions. However, I'll give the @ a try - it's a synonym for this in CoffeeScript, right? – Johann Philipp Strathausen Commented Jun 15, 2011 at 17:05
  • Well, if @ piles to this, I couldn't use it in other functions, like callbacks, since it would refer to that function... – Johann Philipp Strathausen Commented Jun 15, 2011 at 17:11
  • By the way, I now consider using coffee.eval not such a good idea. Instead, one could just use require that will resolve coffe-script files as well. – Johann Philipp Strathausen Commented Aug 29, 2011 at 10:14
Add a ment  | 

4 Answers 4

Reset to default 6

Having someone else's opinion forced on you? It's Hack Time™!

o =
  a: 1
  b: 2
  c: 3

`with(o) {//`
  alert a
`}`

"Compiles" to:

var o;
o = {
  a: 1,
  b: 2,
  c: 3
};
with(o) {//;
alert(a);
};

It's a pity that this is another area where Doug Crockford's opinion is taken as gospel. with Statement Considered Harmful rejects it on the basis of ambiguity of property writes, but ignores its usefulness when used with a read-only context object, such as a context object which defines a DSL-like API.

The Coco fork of CoffeeScript supports a with syntax; see https://github./satyr/coco/wiki/additions. However, that syntax simply sets the value of this to the target object in a block, rather than piling to the problematic and deprecated with keyword.

Let's say that you wanted to emulate Coco's with syntax in CoffeeScript. You'd do something like this:

withObj = (obj, func) -> func.call obj

Then you could write

withObj = (obj, func) -> func.call obj
withObj annoyingly.lengthy.obj.reference, ->
  @foo = 'bar'
  @bar = 'baz'

Of course, in such simple cases, it's better to use a utility function like jQuery or Underscore's extend:

_.extend annoyingly.lengthy.obj.reference, foo: 'bar', bar: 'baz'

No. However, you can use destructuring assignment to achieve a similar effect.

{foo, bar, baz} = require 'toocoollib'
foo 'whoa!'

You should also be aware that the with keyword is deprecated and is a syntax error in ES5 strict mode. The consensus among mainstream JS hackers is that with is a bad idea.

If you posted an example of what you would like to do, I could give a more specific answer.

Just found that on the CoffeeScript FAQ page about "Variable Importing":

https://github./jashkenas/coffee-script/wiki/FAQ

It seems, that the use of this technique is discouraged in CoffeeScript and not supported.

发布评论

评论列表(0)

  1. 暂无评论