How to use the exported variables of another commonjs module without declare them again? - module

Suppose I have a module models.js:
exports.User = mongoose.model('User', UserSchema);
exports.Question = mongoose.model('Question', QuestionSchema);
exports.Answer = mongoose.model('Answer', AnswerSchema);
exports.Comment = mongoose.model('Comment', CommentSchema);
Now I want to use it in another file:
var models = require('./models');
var User = models.User;
var Question = models.Question;
var Answer = models.Answer;
var Comment = models.Comment
// then use them
var user = new User();
It is boring that I have to declare all the models I defined in models.js.
Is there any way to simplify it, that I don't need to declare the models again:
var models = require('./models');
// !!! do some magic
var user = new User();

Curious why don't you want to simply do with no magic needed - what is issue with simple ..
var models = require('./models');
var user = new models.User();

Related

Dynamic parameter in parsed expression

A quick sample to understand my situation:
static Interpreter ParseInterpreter = new Interpreter();
...
var func = ParseInterpreter.Parse("ctx.SomeProp", new Parameter("ctx", typeof(???1)).Compile<Func<???2, object>>;
...
var token = JToken.Parse(s);
dynamic dToken = token;
var obj = func(dToken);
In other words, is there a way to pass some dynamic parameter to Parse method and then get a functor which accepts such parameters?
You can directly use the Lambda class returned by the Parse method, and not call the Compile function:
var interpreter = new Interpreter()
string expression = "ctx.SomeProp";
Lambda parsedExpression = interpreter.Parse(expression, new Parameter("ctx", typeof(object)));
var token = JToken.Parse(s);
var result = parsedExpression.Invoke(token);
I have not tested exactly your code but for example I have a test like this that works correctly:
dynamic dyn = new ExpandoObject();
dyn.Foo = "bar";
var interpreter = new Interpreter()
.SetVariable("dyn", (object)dyn);
Assert.AreEqual(dyn.Foo, interpreter.Eval("dyn.Foo"));
Consider that this only works on .NET 4.x, on .NET Standard/Core dynamics are not supported.

How to Set with Priority in Angularfire

I'm having trouble understanding how to set with $priority in angularfire. I am trying to add a username with the key as username.
For example if I try:
var object = {user : "name",
$priority : "this"};
var ref = new Firebase(FIREBASE_URL + 'users');
var newBar = $firebase(ref);
newBar.$set(username, object);
The firebase set fails because of the invalid character "$" in priority.
I understand that instead I could try :
var object = {user : "name",
$priority : "this"};
var ref = new Firebase(FIREBASE_URL + 'users');
var newBar = $firebase(ref).$asArray();
newBar.$add(object);
This succeeds in adding the object to the array, but doesn't give me the opportunity to set the key to username as I require.
I can't think of any other way to achieve this currently. Is there any way to use set that allows me to set $priority? Or any alternative method to achieve the same?
Thanks
$priority is a property that exists on synchronized objects and records inside a synchronized array. As you've noted, it's not an allowed key in firebase data, so using it with $firebase::$set doesn't make sense here (since $set takes a valid json object which is stored directly into Firebase). Reading the API specifications can be a big help here.
There is also no need to create a synchronized binding for this use case. Just use the existing Firebase reference:
var ref = new Firebase(URL);
ref.child(username).set(object, function(error){ /* ... */ });
If there is some use case for working within a synchronized binding, then just use the Firebase meta property .priority:
var object = {user: "name", ".priority": "this"};
var ref = new Firebase(URL);
var sync = $firebase(ref);
sync.$set(object).then(/* ... */);
In case anyone else is looking at this, I have now taken the approach of using set first and then setting priority thereafter like this:
var object = {user : "name",
$priority : "this"};
var username = "user1";
var ref = new Firebase(FIREBASE_URL + 'users');
var newBar = $firebase(ref);
newBar.$set(username, object).then(function(){
var ref2 = new Firebase(FIREBASE_URL + 'users/' + username);
var newBar2 = $firebase(ref2).$asObject();
newBar2.$loaded().then(function(){
newBar2.$priority = authUser.uid;
newBar2.$save();
});
});

how to use serialization package

I want to convert my class to a Map so I'm using Serialization package. From the example it looks simple:
var address = new Address();
address.street = 'N 34th';
address.city = 'Seattle';
var serialization = new Serialization()
..addRuleFor(Address);
Map output = serialization.write(address);
I expect to see an output like {'street' : 'N 34th', 'city' : 'Seattle'} but instead it just output something I-don't-know-what-that-is
{"roots":[{"__Ref":true,"rule":3,"object":0}],"data":[[],[],[],[["Seattle","N 34th"]]],"rules":"{\"roots\":[{\"__Ref\":true,\"rule\":1,\"object\":0}],\"data\":[[],[[{\"__Ref\":true,\"rule\":4,\"object\":0},{\"__Ref\":true,\"rule\":3,\"object\":0},{\"__Ref\":true,\"rule\":5,\"object\":0},{\"__Ref\":true,\"rule\":6,\"object\":0}]],[[],[],[\"city\",\"street\"]],[[]],[[]],[[]],[[{\"__Ref\":true,\"rule\":2,\"object\":0},{\"__Ref\":true,\"rule\":2,\"object\":1},\"\",{\"__Ref\":true,\"rule\":2,\"object\":2},{\"__Ref\":true,\"rule\":7,\"object\":0}]],[\"Address\"]],\"rules\":null}"}
Serialization is not supposed to create human-readable output. Maybe JSON output is more what you look for:
import dart:convert;
{
var address = new Address();
..address.street = 'N 34th';
..address.city = 'Seattle';
var encoded = JSON.encode(address, mirrorJson);
}
Map mirrorJson(o) {
Map map = new Map();
InstanceMirror im = reflect(o);
ClassMirror cm = im.type;
var decls = cm.declarations.values.where((dm) => dm is VariableMirror);
decls.forEach((dm) {
var key = MirrorSystem.getName(dm.simpleName);
var val = im.getField(dm.simpleName).reflectee;
map[key] = val;
});
return map;
}
The new Address() creates a full prototype object which is what you are seeing. That being said, they could have done something to avoid part of those, but if you want to restore the object just the way it is, that's necessary.
To see the full content of an object you use the for() instruction in this way:
for(obj in idx) alert(obj[idx]);
You'll see that you get loads of data this way. Without the new Address() it would probably not be that bad.
Serialization won't help you here...
You might give a try to JsonObject library, and maybe go through this in depth explanation how to do what you are trying to do using mirrors.

Orchard - Creating Query Programmatically

I am creating a custom module in Orchard. After I enable my module I would like to create a query programmatically.
I do that in my Migrations.cs file thanks to implementation of IDependency interface.
I am able to create the query but I do I programmatically set filters of that query?
var announcementsQuery = _contentManager.Create("Query");
announcementsQuery.As<TitlePart>().Title = "Announcements";
_contentManager.Publish(announcementsQuery);
I found out how to do this:
var announcementsQuery = _contentManager.Create("Query");
announcementsQuery.As<TitlePart>().Title = "Announcements";
announcementsQuery.As<QueryPart>().ContentItem.ContentType = "Announcement";
var filterGroupRecord = new FilterGroupRecord();
var filterRecord = new FilterRecord()
{
Category = "Content",
Type = "ContentTypes",
Description = "Announcement",
Position = 1,
State = "<Form><Description>Announcement</Description><ContentTypes>Announcement</ContentTypes></Form>"
};
filterGroupRecord.Filters.Insert(0, filterRecord);
announcementsQuery.As<QueryPart>().FilterGroups.Insert(0, filterGroupRecord);

Create abstract complexType as an input to an operation for webservice argument class - Coldfusion

I ran into the error:
Unable to create web service argument class [Lcom.verticalresponse.api._1_0.VRAPI_xsd.NVPair;. Error: java.lang.InstantiationException: [Lcom.verticalresponse.api._1_0.VRAPI_xsd.NVPair;. Often this is because the web service defines an abstract complexType as an input to an operation. You must create an actual instance of this type in Java.
while trying to call a WebService. And I have the solution now and I want to post it for anyone else who might be in my situation one day. The issue is that you have to pass a complex structure through coldfusion to the webservice and it doesn't always like to play well.
The answer to this conundrum is how you set up the data as a combination of structures and arrays.
you have to build the data in a very particular manner.
<cfscript>
var ELMResults = StructNew();
var ELMArgs = StructNew();
var MemberData = arrayNew(1);
var ListMember = StructNew();
var session_id = 'the_session_id_provided'
var list_id = 'the_list_id_provided';
var list_name = 'the_list_name_provided';
var list_type = 'the_list_type_provided';
/* set session id */
ELMArgs.session_id = session_id;
/* set member data */
MemberData[1] = StructNew();
MemberData[1].name = "hash";
MemberData[1].value = hash_value;
MemberData[2] = StructNew();
MemberData[2].name = "optin_status";
MemberData[2].value = "2";
MemberData[3] = StructNew();
MemberData[3].name = "first_name";
MemberData[3].value = "Chewbacca";
MemberData[4] = StructNew();
MemberData[4].name = "fax";
MemberData[4].value = "1112223333";
MemberData[5] = StructNew();
MemberData[5].name = "email_address";
MemberData[5].value = email_address;
/* set list member details */
ListMember.list_id = list_id;
ListMember.list_name = list_name;
ListMember.list_type = list_type;
ListMember.member_data = MemberData;
ELMArgs.list_member = ListMember;
ELMResults = VR.editListMember(ELMArgs);
</cfscript>
I hope this helps someone because I was scratching my head for a while.
I found the solution to my own question and posted it here with the question because I couldn't post an answer on the same day of asking the question. I am now simply closing out the thread. I hope this answer helps someone because I was scratching my head for a while with this one.
Cheers, JP