In Strong loop i have a User model, and a Messages model. The Messages model has a custom method added in the message.js file. Lets say it's called
Message.sendMessage
This works fine at the messages end point. I've then added the messages model to the User model as a one to many - this works fine and i can now see all the messages models that are predefined, but not my methods added via actually code. So the basic CRUD methods work, but not custom methods. Any idea how to make these show up?
Message.sendMessage is a static method and cannot be invoked from a model instance.
If you want to invoke it from the instances of the Message class, you need to define it as Message.prototype.sendMessage
Related
I'm trying to refactor a codebase that was passing domain models to the views, to use DTOs instead.
Overall everything looks cleaner now, except one point: I have some business logic that belongs to the domain but needs to be executed in the view as well.
Example: I have an Activity class, that has a method to check whether a given User can edit it:
class Activity {
public function isEditableBy(User $user): bool {
...
}
}
The domain service layer uses this method to ensure that the Activity can be edited, and throw an exception if not.
The view uses this method to display an Edit button if the Activity is editable by the user.
Question: How to avoid duplicating this logic in the DTO and the model?
I thing you should not put logic in the DTO at all due to its definition "Data Transfer Object". So, "data" not "logic". Instead you can calculate result of the isEditableBy() method in your application service layer, put this result into a DTO and use the DTO in the view layer.
I am fairly new to ember. I have an existing Ember App and i need to implement a functionality in it. I have a Ember Object as below
`import Ember from 'ember'`
CallService = Ember.Object.extend
... other code here
_updateConnectedLeadId: (->
console.log "Do i get here??"
**pass the route action here**
).observes('some_other_here')
`export default CallService`
Unfortunately, i couldn't put the whole code here.
My route looks like
ApplicationRoute = Ember.Route.extend
actions:
showLead: ->
console.log data
console.log "did i get here?"
#transitionTo('dashboard')
`export default ApplicationRoute`
I tried using #send('showLead'), #sendAction('showLead') in my method but no luck.
My main intention is to make a transition once the console.log "Do i get here??" is displayed. I am not sure if i am on the right way here.
I also tried using #transitionTo('dashboard') and #transitionToRote('dashboard') directly but it throws me errors.
I have been stuck for a day on this now and i am clueless.
I'll be grateful for any guidance and help. Thanks
You have the problem that you are trying to trigger a route action or trigger a transition from within an Ember.Object, named call service. The code you create is unclear about where your custom object is being created; where the observer is triggered due to a change to object's property update, and so on.
Nevertheless, I tried to provide a working example for you. If you open the twiddle, you will see that I created a my-object instance within index.js route and pass it as model to my-component. When you click the button within my-component.hbs. The my-object instances dummyVariable is toggled and the observer within my-object executes. The tricky part here is that I passed index route itself as ownerRoute property to my-object instance; so that I can trigger the index route's dummyAction from within my-object.js with
ownerRoute.send('dummyAction');
so that related action executes and transition to my-route is performed. Although, I believe this might solve your question; I am not quite happy about the design. I do not think, it is a good way for Ember.Objects to know about routes, actions, controllers, etc. I believe the proper way is observing object's relevant properties from within this constructs and perform necessary actions by their own. Moreover, you might consider creating a service instead of an object and inject the service directly to your route instead of creating an instance of your class extending Ember.Object. If you have to extend from Ember.Object you can just inject relevant route, controller, etc to the instances of that particular object class by using an instance-initializer if you need.
Anyway, please take a look at the twiddle and ask more if you need to. I will be happy to help if I can.
again I've got a question about Kohana and how I am supposed to use model functions.
I want to move parts of a controller function into a more appropriate model to be able to access this function from additional controllers. (From what I have read so far I conclude that calling the controller function from a different controller is considered bad architecture).
My problem is that depending on several circumstances (i.e. model parameters) this controller function creates a log entry in a different database table and sends an email to some users.
How am I supposed to create this log entry and send the mails if main functionality resides inside the model? Should I instantiate the second model from within the first, call the log function and afterwards send the mails exactly how I did from my controller?
Thanks in advance.
This is a question that doesn't have 1 correct answer, unfortunately. A lot of it comes down to how you prefer to implement the MVC pattern.
At its base MVC uses: Models, Views and Controllers
Models
These classes should represent entities in your database
Example:
Model_User maps to an entity in your Users table
$user = new Model_User;
$user->first_name = 'Joe';
$user->last_name = 'Smith';
$user->save();
Views
These files store presentation data/templates (html usually/mostly)
Example:
index.tpl
<h1>HELLO, WORLD!</h1>
<h2><?=$some_variable_from_controller?></h2>
Controllers
These files handle incoming requests and process data to be injected into views
Example:
Controller_Home handles request to the home page
class Controller_Home extends Controller {
public function action_index(){
$view = View::factory('index');
$view->render();
}
}
Now that you get the basics it's time to understand a specific problem this limited structure promotes. Controllers get kind of fat and messy. This leads us to libraries or a service oriented architecture.
These libraries allow us to move large groups of logic into a portable service layer that can easily be used across all controllers, models and other libraries. They also break our code up into smaller more concise chunks that actually made sense.
Example:
In my controller instead of using a bunch of logic that logs a user in via facebook I can simply create a Social_Login_Service and use it as follows.
Social_Login_Service::facebook($user_email);
Now you would simply see that 1 clean line in your login controller instead of a whole messy slew of logic that will pile up in your controller until it melts your brain to looks at.
This is a very basic overview of a possible direction (and one that I prefer).
It is very useful to break up your sites into smaller components and if you're using Kohana, I recommend doing this with Modules (http://kohanaframework.org/3.1/guide/kohana/modules) they're great.
I hope this little snippet helped.
I m working on a user interface in JavaFX. It is the front-end of an infrastructure service that I have already developed and is operational. I read here and there that mock can be used to avoid running all the system when it is to heavy to run, but also for isolation purpose.
At present I want to run some basic test as I am also learning how to use JavaFX and I would not want to run all my infrastructure for the matter.
Basically I have a TreeView that I would like to update based on what is coming from the service. Normally the service that runs in background would update the model and call a Platform.runlater() method to ask the UI to refresh.
I was wondering how can I achieve that using mocking. How can I have a mock object update a simplified shared structure such as a list(model) and then call Platform.runlater()? Actually I would first ask: is it a possible and appropriate usage of mock and if yes how can it be done, with which framework?
Personally what is a bit not clear to me is the involvement of multi-threading. Indeed my object under test which is the interface would not be calling any method of my mock, expect indirectly, the Run() method, given that my service is a runnable.
Hence I would appreciate if anyone could enlighten me a bit further on the matter. I'm confused....
Best,
Maatari
First prepare applications (or change your current app to support test mode) which can populate your TreeView with specific data. The entity which will provide fake data for TreeView will be your mock object for specific data. It should look like a service from TreeView point of view, which means you need to extract common methods from your services class into interface and make TreeView use this interface instead of concrete class.
To handle UI testing you can you use JemmyFX library. You can create simple test which will verify any UI properties of your TestView or imitate user actions like mouse clicks or text input. You may not worry about threading issues, jemmy will handle it for you.
It can look next way (I use junit as test harness here):
public class TreeTest {
#BeforeClass
public static void setUpClass() throws Exception {
// running your specially crafted FX application with mock service data
// you can do it any way, e.g. by calling main() method with some parameters
AppExecutor.executeNoBlock(TreeApp.class);
}
#Test // this is junit annotation for test
public void fooTest() {
// here we are receiving special jemmyfx entity which knows how to handle
// tree views and is attached to your TreeView
// (if you app has only one TreeView, you may need additional logic for several ones)
TreeViewDock tree = new TreeViewDock(new SceneDock().asParent());
// this way we can find some item which you expected to see in you TreeView
// because you've created you mock data this way.
// Note that underlying code will respect UI threading,
// will understand that UI can have delay and will give it certain time without blocking
// and will throw descriptive exception in case expected item wasn't found
tree.asTree().selector().select(new ByToStringLookup("item1"));
// you can find subitems
tree.asTree().selector().select(new ByToStringLookup("item1"), new ByToStringLookup("subitem1");
// and perform operations on them, e.g. expand:
new TreeItemDock(tree.asItemParent(), new EqualsLookup("item2")).asTreeItem().expand();
//etc
}
You can find more info on site on just by code completion hints
I've created a cutom BindingNavigator with custom ToolStripButton(s), basically working with datasets, in which I've coded delegate actions to perform when adding, deleting and updating records.
As I'm moving to EF5, I would like to make the same thing, the idea still the same, but when executing these three methods:
this.BindingSource.AddNew();
this.BindingSource.RemoveCurrent();
this.BindingSource.Update();
modifications are made in runtime but not in database.
I found later that i must execute
context.SaveChanges();
to make data concrete.
Can anyone help on how to call the context from the custom binding navigator class.