Skip to content

Instantly share code, notes, and snippets.

@csharpforevermore
Created December 6, 2019 12:10
Show Gist options
  • Save csharpforevermore/40cf171f620e0214569e102ec0cfb44b to your computer and use it in GitHub Desktop.
Save csharpforevermore/40cf171f620e0214569e102ec0cfb44b to your computer and use it in GitHub Desktop.
Why is this Property Value Converter still having Umbraco v8.3.0 ModelsBuilder output type of 'object' rather than 'bool'
angular.module('umbraco').directive('switch', function () {
return {
restrict: 'AE',
replace: true,
transclude: true,
template: function (element, attrs) {
var html = '';
html += '<span';
html += ' class="switcher' + (attrs.class ? ' ' + attrs.class : '') + '"';
html += attrs.ngModel ? ' ng-click="' + attrs.ngModel + '=!' + attrs.ngModel + '"' : '';
html += ' ng-class="{ checked:' + attrs.ngModel + ' }"';
html += '>';
html += '<small></small>';
html += '<input type="checkbox"';
html += attrs.id ? ' id="' + attrs.id + '-check"' : '';
html += attrs.name ? ' name="' + attrs.name + '"' : '';
html += attrs.ngModel ? ' ng-model="' + attrs.ngModel + '"' : '';
html += ' style="display:none" />';
html += '</span>';
return html;
}
}
});
angular.module("umbraco").controller("ESO.TrueFalse.Controller", function ($scope, $timeout, angularHelper) {
var alreadyDirty = false;
$scope.model.textRight = "";
if ($scope.model.value === null || $scope.model.value === "") {
$scope.enabled = 0;
} else {
$scope.enabled = $scope.model.value == 1;
}
$scope.$watch('enabled', function (newval, oldval) {
$scope.model.value = newval === true ? 1 : 0;
if ($scope.model.value == 1) {
$scope.model.textRight = "Yes";
}
else {
$scope.model.textRight = "No";
}
if (newval !== oldval) {
//run after DOM is loaded
$timeout(function () {
if (!alreadyDirty) {
var currForm = angularHelper.getCurrentForm($scope);
currForm.$setDirty();
alreadyDirty = true;
}
}, 0);
}
}, true);
});
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Umbraco.ModelsBuilder v8.1.0
//
// Changes to this file will be lost if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.ModelsBuilder;
using Umbraco.ModelsBuilder.Umbraco;
/// <summary>Global Settings</summary>
[PublishedModel("globalSettings")]
public partial class GlobalSettings : PublishedContentModel
{
...
///<summary>
/// Enable
///</summary>
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder", "8.1.0")]
[ImplementPropertyType("enable")]
public object Enable => this.Value("enable");
...
}
Picture of Data Type, pointing at the Property Editor
https://pasteboard.co/IJZsSoM.png
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web;
using System;
public class TrueFalseValueConverter : Umbraco.Core.PropertyEditors.PropertyValueConverterBase
{
public override bool IsConverter(IPublishedPropertyType propertyType) => propertyType.EditorAlias.Equals("ESO.TrueFalse", StringComparison.InvariantCultureIgnoreCase);
public virtual object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
var attemptConvertInt = source.TryConvertTo<bool>();
if (attemptConvertInt.Success)
{
return attemptConvertInt.Result;
}
return null;
}
public virtual object ConvertSourceToXPath(
PublishedPropertyType propertyType,
object source,
bool preview)
{
if (!(bool)source)
return (object) source.ToString();
return (object) false;
}
}
<div ng-controller="ESO.TrueFalse.Controller" class="usn-cms">
<switch name="enabled" ng-model="enabled" id="{{model.alias}}"></switch>
<span class="switcher-status" ng-class="{ active: enabled }">{{model.textRight}}</span>
</div>
@csharpforevermore
Copy link
Author

Turns out it was missing a method

public override Type GetPropertyValueType(IPublishedPropertyType propertyType) => typeof(bool);

@CarlSargunar
Copy link

Is there an interface we could tie this up with? That would prevent this sort of thing happening to future unsuspecting developers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment