Change the way a DataProperty displayName is looked up in a Validator in Breeze - breeze

In the Validator class, the rootContext has a displayName function that is used to find the display name when constructing the validation error message. I'm storing user-friendly display names in the custom properties object of all my DataProperty's, so I'd like that displayName function to look in that area before any other area. As far as I can tell there is no way to override the rootContext from the Validator class in order to change it there. And the only other way I can think to do it would be to pass in a new displayName function in the context when I'm constructing the Validators, but in order to do that I'd have to write a custom metadata parser, unless I'm missing something there. Do you guys have a suggested way of doing this?

It is good question. This is NOT well documented but ... for the time being I would just copy the value resulting from executing your custom displayName function directly onto the 'displayName' property of the corresponding DataProperty or NavigationProperty immediately after querying your metadata. We will be documenting the 'displayName' property in the next release but it is available now.
The subject of improving 'displayName' discovery process is something that is on our plate but we just haven't gotten to it yet.

Related

typo3 flow isDirty on model

Im trying to find out which attributes of an entity have been changed.
As far I have seen, there is a PersistenceSession with a method to check an object if an attribute isDirty. But its always true because it never registers the old object.
So if I take the demo from the QuickGuide and override the update method in the CoffeeBeanRepository:
/**
* #param \Acme\Demo\Domain\Model\CoffeeBean $coffeeBean
*/
public function update($coffeeBean) {
\TYPO3\Flow\var_dump($this->persistenceSession->isDirty($coffeeBean, 'name'), "name changed before");
parent::update($coffeeBean);
\TYPO3\Flow\var_dump($this->persistenceSession->isDirty($coffeeBean, 'name'), "name changed after");
}
... its always TRUE (both), despite I didn't change anything.
Anyone an idea/reference how this can be accomplished?
I am using it for a REST API where a user can't update several fields and on editing of some fields additional actions have to be executed.
The persistenceSession is part of the generic persistence backend of Flow and is neither maintained, nor really used unless you explicitly deactivate doctrine. Hence persistenceSession will not help you, because all entities are considered new for the persistenceSession as you noticed.
With doctrine you need to get the entity changeset from the "UnitOfWork", which you can get from an injected \Doctrine\Common\Persistence\ObjectManager. See also Is there a built-in way to get all of the changed/updated fields in a Doctrine 2 entity
However, this is a suboptimal solution and a hacky work-around at best. If you need to track changes to your entity, it should be an explicit part of your domain model. For example make your setters record a changed properties list, when the given value is different from the current.
When done, you could even optimize doctrines change tracking on the way with that: http://doctrine-orm.readthedocs.org/en/latest/reference/change-tracking-policies.html#notify

Why dose many MVC html Helpers uses Delegates as input parameters

In MVC if you want to create an editor for a property or a display for you property you do something like that:
#Html.EditorFor(m=> m.MyModelsProperty);
#Html.DisplayFor(m=> m.MyModlesProperty);
Why do we have to pass a delegate why can't we just pass the model's property directlly? e.g.:
#html.EditorFor(Model.MyModlesProperty);
The reason for this is because of Metadata. You know, all the attributes you could put on your model, like [Required], [DisplayName], [DisplayFormat], ... All those attributes are extracted from the lambda expression. If you just passed a value then the helper wouldn't have been able to extract any metadata from it. It's just a dummy value.
The lambda expression allows to analyze the property on your model and read the metadata from it. Then the helper gets intelligent and based on the properties you have specified will act differently.
So by using a lambda expression the helper is able to do a lot more things than just displaying some value. It is able to format this value, it is able to validate this value, ...
I'd like to add, that besides the Metadata and making the Html helper strongly typed to the Model type, there's another reason:
Expressions allow you to know the name of the property without you hard coding strings into your project. If you check the HTML that's produced by MVC, you'll see that your input fields are named "ModelType_PropertyName", which then allows the Model Binder to create complex types that are passed to your Controller Actions like such:
public ActionResult Foo(MyModel model) { ... }
Another reason would be Linq to SQL. Expression Trees are the magic necessary to convert your Lambdas to SQL queries. So if you were to do something like:
Html.DisplayFor(p => p.Addresses.Where(j => j.Country == "USA"))
and your DbContext is still open, it would execute the query.
UPDATE
Stroked out a mistake. You learn something new every day.
The first example provides a strongly-typed parameter. It forces you to choose a property from the model. Where the second is more loosely-typed, you could put anything in it, even something that isn't valid property of the model.
Edit:
Surprisingly, I couldn't find a good example/definition of strong vs loose typing, so I'll just give a short example regarding this.
If the signature was #html.EditorFor(string propertyName); then I could make a typo when typing in the name and it would not be caught until run-time. Even worse, if the properties on the model changed, it would NOT throw a compiler error and would again not be detected until run-time. Which may waste a lot of time debugging the issue.
On the other hand with a lambada, if the model's properties changed you would get a compiler error and you would have to fix it if you wanted to compile your program. Compile-time checking is always preferred over run-time checking. This removes the chance of human error or oversight.

knockout-validation plugin, get isValid from specific property

Is is possible to see if just one property is valid in the knockout-validation plugin?
I could not see any reference to it in the documentation, only the ability to see if the whole model is valid.
For example I wish a computed observable to have a different value depending upon if another observable is valid.
I was being led down the wrong direction, as I was using async validators and over complicated my problem, I think it is as simple as:
propertyName.isValid();
e.g.
self.email.isValid();

Property documentation

Are there any entity.property documentation 'field' in breeze?
For example, on an entity.property in EntityFramework are two documentation properties: Long Description and Summary. I can see those two properties in metadata on client side but I am wondering if any of those properties are used in breeze.
Analyzing the breeze.debug.js I didn't notice any usage of those or similar properties but maybe somebody have an idea how to use them (extract from metadata) and attach them for example to an entity instance. Or maybe somebody have some similar solution.
This is a good idea!. Please add this to the breeze User Voice. We take these suggestions very seriously. Hopefully we can also get out some documentation describing how to intercept the metadata retrieval process so that you can add your own logic to do this.
As a stopgap, the MetadataStore.fetchMetadata method currently does return ( in its promise 'then' method) the the raw "metadata" retrieved from the server. So for now, you could plumb this and pick out these properties and attach them directly to each corresponding breeze dataProperty.
Note that by the time the fetchMetadata method returns the entire MetadataStore will have already been populated with entityTypes, dataProperties, navigationProperties, etc. This makes the task much easier.

Introduce custom property in OData

In my database User table I have DataTime field called DateDeleted - which is null while user exists and is set to the proper value when user "is deleted".
I wonder if there is a way to introduce IsDeleted property for User entity so that
http://odata/service.svc/Users(1)/IsDeleted
will return true or false depending on whether DateDeleted is set or not
My research in google hasn't got any results and I am almost sure it is not possible to implement through odata. Am I right?
With the built in providers this is not possible on the WCF DS side of things. You might be able to somehow do this on the EF side (expose it as a property of the EF entity), but I'm not sure if that's possible.
On the WCF DS side, you would have to implement a custom provider in order to do this. Which may be quite a lot of work unfortunately. If you're interested see this for starters: http://blogs.msdn.com/b/alexj/archive/2010/01/07/data-service-providers-getting-started.aspx.
What Shawn refers to above is a method on the custom provider interface.
You can specify the value you want by implementing the method DataServiceQueryProvider.GetPropertyValue.
Please find the reference here:
http://msdn.microsoft.com/en-us/library/system.data.services.providers.idataservicequeryprovider.getpropertyvalue.aspx
The method takes two parameters, the entity object (a User instance) and the resource property (in this case "IsDeleted"). You can try to get the property value of "DataDeleted" from the entity object, and return the value of "IsDeleted" as you want.

Resources