Reference to component - node.js

Using knockout.js in node, how can I get a reference to a child VM component, which was invoked from a template?
Illustration
I have a Question Model, VM, containining a custom Resource component, with it's respective Model and VM. The Resource VM registers a custom component and receives a Resource Model object as a parameter, which was constructed by the parent Question Model. This constructed resource is passed as a parameter with the template:
QuestionModel.js
this = new QuestionModel(...);
this.resource = new ResourceModel(some data);
question-template.html
<div data-bind="foreach: { data: questions, as: 'question' }">
<!-- question related -->
<resource params="resource: question.resource"></resource>
</div>
ResourceVM.js
define(function(require, exports, module) {
var ko = require('knockout');
var ResourceViewModel = function ResourceViewModel(params) {
this.resource = params.resource;
this.somethingSpecific = function() {
return 'some value manipulate from this model';
}
}
ko.components.register('resource', {
viewModel: {
createViewModel: function(params, componentInfo) {
return new ResourceViewModel(params);
}
},
template: {
require: 'text!/resource-template.html'
}
});
return ResourceViewModel;
});
I want to be able to call functions of the Resource VM (like question.resourceVM.somethingSpecific()).
What is a proper way of getting a reference to a component child?
The only solution I can think of is to pass the parent object with the parameters and extend it from child, which is obviously bad.

Your QuestionModel already has access to this.resource, so the way forward might be by doing it through the data models, instead of through the view models. Having somethingSpecific() as an attribute on either QuestionModel or ResourceModel instead of ResourceViewModel would solve the problem nicely.
I would argue that manipulating data is the responsibility of the entity that holds it; the job of the ResourceViewModel is only to provide glue between the data model and the DOM.
var QuestionModel = function QuestionModel() {
this.somethingSpecific = function somethingSpecific() {
this.resource.doStuff();
};
};
this = new QuestionModel();
this.resource = new ResourceModel(some data);
You could then give your resource component access to the question instead of the child resource:
var ResourceViewModel = function ResourceViewModel(params) {
this.question = params.question;
this.resource = this.question.resource;
}

Related

Why some of the variables are not changed in the nested tag, in RiotJs?

I have a simple nested tag:
<nested-tag>
<p>myTitle: {myTitle}</p>
<p>{myKeyword}</p>
this.myTitle = opts.title;
this.myKeyword = opts.keyword;
</nested-tag>
You can see I assign the opts.title and keyword to two new variable myTitle and myKeyword.
Then I use it in a loop of a parent tag:
<my-tag>
<input type="text" onkeyup={search} value={keyword} />
<ul>
<li each={items}>
<nested-tag title={title} keyword={parent.keyword}></nested-tag>
</li>
</ul>
this.keyword = ""
var initItems = [{ title: "aaaa"}, { title: "bbbb"} ]
this.items = initItems
this.search = function(event) {
this.keyword = event.target.value;
this.items = initItems.filter((item) => item.title.indexOf(this.keyword) >=0 );
}
</my-tag>
You can see I passed the parent.keyword to nested-tag as keyword variable.
When I input something to the text input, the keyword will be changed, so the <nested-tag> will be recreated with the new parent.keyword.
But it's not, the {myKeyword} of nested-tag is always empty. I have to rewrite it with directly opts.keyword invocation:
<nested-tag>
<p>opts.title</p>
<p>opts.keyword</p>
</nested-tag>
And it's working well now.
I'm not sure why and how to fix it? Do I have to always use opts.xxx in the nested tags?
A live demo is here:
http://jsfiddle.net/3jsay5dq/10/
you can type something to the text input to see the result
The javascript in your component nested-tag gets run when instantiating the component. So, when the component is getting generated, the myTitle and myKeyword will be initialized with whatever opts are passed in. But, on update, the myTitle and myKeyword are still pointing to the values set during instantiation. The cleanest way to go about it is to use opts[key] as they will always reflect what is being passed to the component. If you insist on using your own local properties, then you could modify your component like this:
<nested-tag>
<p>myTitle: {myTitle}</p>
<p>{myKeyword}</p>
// this will run every time there is an update either internally or from a passed opts
this.on('update', () => {
this.myTitle = this.opts.title;
this.myKeyword = this.opts.keyword;
})
// this will only run once during instantiation
this.myTitle = opts.title;
this.myKeyword = opts.keyword;
/*
// could be refactored to
this.setMyProps = () => {
this.myTitle = this.opts.title;
this.myKeyword = this.opts.keyword;
}
// bind it to update function
this.on('update', this.setMyProps)
// run once for instantiation
this.setMyProps()
*/
</nested-tag>

Initialize a Blockly Mutator within JavaScript

Hi,
as far as I know, custom blocks in Blockly can be defined wether in JSON or in JavaScript, but how can a mutator be initialized in JavaScript?
with JSON:
Blockly.defineBlocksWithJSONArray([
{....
"mutator": "myMutatorName"
});
Then the Mutator_MIXIN must be defined and with Blockly.Extension.registerMutator('myMutatorName', Blockly.myMutator_MIXIN, null, null) the mutator is added to the Block.
with JavaScript:
Blockly.Blocks['blockName'] = {
init: function() = {
....
??? this.setMutator(???)???
};
}
So how can this be done in JavaScript?
Kind regards
a new one
I might be just a little bit late here, but I'll leave the answer anyway for those who need a bit more concrete example.
In JavaScript, you don't actually need to bind a mutator to your block, you just need to define mutationToDom() and domToMutation(xmlElement) functions, like so:
Blockly.Blocks['my_custom_block'] = {
init() {
// Define your basic block stuff here
},
// Mutator functions
mutationToDom() {
let container = document.createElement('mutation');
// Bind some values to container e.g. container.setAttribute('foo', 3.14);
return container;
},
domToMutation(xmlElement) {
// Retrieve all attributes from 'xmlElement' and reshape your block
// e.g. let foo = xmlElement.getAttribute('foo');
// this.reshape(foo);
},
// Aux functions
reshape(param){
// Reshape your block...
}
}
Blockly will automagically take care of the rest and allow you to treat your block as dynamic one.
And if you need to used Mutator Editor UI, you must define decompose(workspace) and compose(containerBlock) functions and call this.setMutator(...) to set which blocks are used in the Mutator Editor UI, like so:
Blockly.Blocks['my_custom_block'] = {
init() {
// Define your basic block stuff here
// Set all block that will be used in Mutator Editor UI, in this
// case only 'my_block_A' and
this.setMutator(new Blockly.Mutator(['my_block_A', 'my_block_B']));
},
// Mutator functions
mutationToDom() {
// Same as previous example
},
domToMutation(xmlElement) {
// Same as previous example
},
decompose(workspace) {
// Decomposeyour block here
},
compose(containerBlock) {
// Compose your block here
},
// Aux functions
reshape(param){
// Same as previous example
}
}
Hope that these short examples help someone :)
You have to declare how the xml is loaded to dom, and how it is saved to xml and redrawn. Also notice how it attaches a mutator to a block element in case that is the only part you need to reference a mutator already present.
init: initFunction (Like you have declared.)
mutationToDom: MutationToDom,
domToMutation: DomToMutation,
updateShape_: UpdateShape`
If all you require is to create a reference to a mutator then what you need is an element of this kind, which we will programatically create in a bit:
<mutation mutator_name="true"></mutation>
The following snippet is an example of the extra functions mutationToDom, DomtoMutation UpdateShape which attaches extra input conditionally. I have a block with a checkbox that when enabled, adds an extra input.
function MutationToDom() {
var container = document.createElement('mutation');
var continueOnError = (this.getFieldValue('HasCONTINUE') == 'TRUE');
container.setAttribute('continueOnError', continueOnError);
return container;
}
function DomToMutation(xmlElement) {
var continueOnError = (xmlElement.getAttribute('continueOnError') == 'true');
this.updateShape_(continueOnError);
}
function UpdateShape(continueOnError) {
// Add or remove a Value Input.
if (continueOnError) {
this.appendValueInput("CONTINUE_ON_ERROR")
.setCheck('CONTINUE_ON_ERROR');
} else {
if (this.childBlocks_.length > 0) {
for (var i = 0; i < this.childBlocks_.length; i++) {
if (this.childBlocks_[i].type == 'continue_on_error') {
this.childBlocks_[i].unplug();
break;
}
}
}
this.removeInput('CONTINUE_ON_ERROR');
}
}

Fabricjs - clarification with extending toObject method with additional attributes

I got the following code form fabric JS site as mentioned this code is for extending rectangle class with additional property, But I can't understand how it works clearly can someone please explain this piece of code
var rect = new fabric.Rect();
rect.toObject = (function(toObject) {
return function() {
return fabric.util.object.extend(toObject.call(this), {
name: this.name
});
};
})(rect.toObject);
canvas.add(rect);
rect.name = 'trololo';
This code is not extending the toObject method of all the fabric.Rect(s).
Is overwriting the toObject method of your particular instance of fabric.Rect, hosted in the rect var you just created.
(function(toObject) {
return function() {
return fabric.util.object.extend(toObject.call(this), {
name: this.name
});
};
})(rect.toObject);
fabric.util.object.extend is like lodash merge. It takes an object as first argument and adds to it the properties of the object from the second argument.
to.Object.call(this) is calling the function toObject that is the first argument of the wrapping function, that is rect.toObject before being modified.
Fabric support this functionality without modifying the code but using:
rect.toObject(['name']);
Or a nicer way to write this would be:
var originalToObject = fabric.Rect.prototype.toObject;
fabric.Rect.prototype.toObject = function(additionalProps) {
var originalObject = originalToObject.call(this, additionalProps);
originalObject.name = this.name;
return originalObject;
};
In this case can be more clear.

Search information by user on firebase

This is probably very simple ,so sorry...
I'm trying to create an application where i can create users and store informations by Firebase. There is an example here :
http://image.noelshack.com/fichiers/2015/11/1426182307-log.png
But now I want to check if the name already exists if someone wants to create a new user.
How would I go about grabbing individual user information without knowing their id , like simplelogin:54?
I've found this topic Get users by name property using Firebase but it is not the same thing because in my case I don't know the children after "Users"
Cheers,
Like Frank said, you must know something about the user to be able to look her/him up.
However, here is a general answer. I am assuming that by "name" you mean the property "identifiant" that you've created.
Suggestions
Start by looking over the Firebase Query documentation.
Short Answer
To check if a user exists by the identifiant property, you'd orderByChild("identifiant") and query for a specific user with the .equalTo("<identifient_here>").
For example, to check if a user with identifient="aaa",
var usersRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/Users");
var identifient = "aaa";
usersRef.orderByChild("identifiant").equalTo(identifient).once("value", function(snapshot) {
console.log("Loaded user by identifient:",identifient,snapshot.val());
});
If instead you want to query by the key (such as simplelogin:53), you could query by using orderByKey() instead of orderByChild()... or just simply setting the ref to the user's key like so:
var userKey = 'simplelogin:53';
var userRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/Users" + userKey);
userRef.once("value", function(snapshot) {
console.log("Loaded user",snapshot.val());
});
Long(er) Answer
You can handle this with a user factory (see Angular Providers documentation).
You return a promise in the factory using the $q service.
Here is the Angular API documentation for $q.
Example with UserFactory
Check out this working PLNKR example.
It's tied to one of my public Firebase instances.
I created the same simplelogin:53 user in /Users like you have.
If you search for the identifient = aaa, you'll get the right user.
The controller implementation here is for example purposes, and doesn't really do anything worth while. It's just for reference.
The Data
{
"Users" : {
"simplelogin:53" : {
"identifiant" : "aaa"
}
}
}
UserFactory
.factory('UserFactory', function($q, $firebaseObject, fbUrl){
return function(userId){
var deferred = $q.defer();
if (userId.isNotEmpty()) {
var userRef = new Firebase(fbUrl + '/Users/').orderByChild("identifiant").equalTo(userId);
userRef.once("value",
function(dataSnapshot){
if (dataSnapshot.val()) {
console.log("Loaded user",dataSnapshot.val());
deferred.resolve(dataSnapshot.val());
} else {
console.info("Couldn't find user by id",userId);
deferred.reject("No user found by identifient = '"+userId+"'");
}
},
function(error){
console.error("Error loading user",error);
deferred.reject(error);
}
);
} else {
deferred.reject("No id entered!");
}
return deferred.promise;
}
})
Controller
.controller('HomeController',function($scope, UserFactory) {
$scope.identifient = '';
var showError = function(errorMessage) {
if (errorMessage) {
showUser(false);
$scope.error = errorMessage;
} else {
delete $scope.error;
}
}
var showUser = function (userObject) {
if (userObject) {
showError(false);
$scope.user = userObject;
} else {
delete $scope.user;
}
}
$scope.loadUser = function() {
var userPromise = new UserFactory($scope.identifient);
userPromise.then(function(data){
showUser(data);
}).catch(function(error){
showError(error);
});
}
})
Template
<div ng-controller="HomeController">
<h2>Home Template</h2>
<input ng-model="identifient" placeholder="identifient"/>
<button ng-click="loadUser()">Find User</button>
<hr/>
<div ng-if="user">User: {{user}}</div>
<div ng-if="error">Error: {{error}}</div>
</div>
Hope that helps.

FabricJs - How do I Add properties to each object

I need to introduce few additional properties to existing object properties set.
like:
ID
Geo Location
etc
Whenever I draw a shape, I need to add additional properties to the shape and need to get from toDataLessJSON()
As of version 1.7.0 levon's code stopped working. All you need to do is to fix as follows:
// Save additional attributes in Serialization
fabric.Object.prototype.toObject = (function (toObject) {
return function (properties) {
return fabric.util.object.extend(toObject.call(this, properties), {
textID: this.textID
});
};
})(fabric.Object.prototype.toObject);
You have to receive properties argument and pass it on to toObject.
Here's a code for adding custom properties and saving them in JSON serialization for any object on canvas. (I used standard javascript object properties, but it works for me)
canvas.myImages = {};
fabric.Image.fromURL('SOME-IMAGE-URL.jpg', function(img) {
canvas.myImages.push(img);
var i = canvas.myImages.length-1;
canvas.myImages[i].ID = 1; // add your custom attributes
canvas.myImages[i].GeoLocation = [40, 40];
canvas.add(canvas.myImages[i]);
canvas.renderAll();
});
And you then include the custom attribute in object serialization.
// Save additional attributes in Serialization
fabric.Object.prototype.toObject = (function (toObject) {
return function () {
return fabric.util.object.extend(toObject.call(this), {
textID: this.textID
});
};
})(fabric.Object.prototype.toObject);
// Test Serialization
var json = JSON.stringify(canvas.toDatalessJSON());
console.log(json);
canvas.clear();
// and load everything from the same json
canvas.loadFromDatalessJSON(json, function() {
// making sure to render canvas at the end
canvas.renderAll();
}

Resources